日期:2014-05-18  浏览次数:20513 次

请教高手一个DataGrid和一个RadioButtonList的问题
我要做一个网上调查,可以对不同问题进行调查,但每个问题要调查项目数不确定(是在数据库中维护的),每个调查项都是分“好”、“较好”、“中”、“较差”、“差”五种选项。我想实现:通过对不同问题的选择,生成一个调查页面,页面中包含一个DataGrid控件,用来显示不同调查项,并在每一项后包含一个RadioButtonList。但实现时不知如何在DataGrid中加入RadioButton,我试中将问题选项字段转换为模板列,然后加入RadioButtonList,但在提交页面时用DataGrid控件的Row[i].Cell[j].Text属性获得选择项的值时,得到的都是空值。请都高手,如何能解决这个问题呢?
        另外,我想让GridData的某列隐藏起来,但还能获得其值,如何实现?(我将该列的Visible设置为False后,取其值也为空。)

------解决方案--------------------
如果你的提交按钮在外部,那么你的没一项前面应该有个复选框来进行选择了,对么

可以这样...

private void btnSubmit_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in DataGrid1.Items)
{
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl( "RadioButtonList1 ");

if(RadioButtonList1 != null)
{
if(RadioButtonList1.SelectValue > -1)
{
//你自己的操作
//这里可以用DataGrid1.DataKeys[dgi.ItemIndex].ToString() 得到当前行的主键值,但别忘了之前给DataGrid设置DataKeyField
}
}
}

}
------解决方案--------------------
protected void btnSubmit_Click(object sender, EventArgs e)
{
RadioButtonList RadioButtonList1 ;
foreach (GridViewRow r in GridView1.Rows)
{
RadioButtonList1 = (RadioButtonList)r.FindControl( "RadioButtonList1 ");
if (RadioButtonList1 != null)
{
//根据 RadioButtonList1.SelectValue取值或者RadioButtonList1.SelectItem.Text去文本
//使用 GridView1.DataKeys[r.RowIndex].Value.ToString() 得到当前行键值
//记得给GridView1设置DataKeyNames属性
}
}
}
------解决方案--------------------
不使用模版列不能使用RadioButtonList

还是习惯使用模版列吧,是个好东西