高分求助!很奇怪的问题!
在gridview里用findcontrol方法遍历寻找RadioButton,看RadioButton是否被选中若被选中则给变量str赋值,但是很奇怪以下代码无反应!请高手帮忙!foreach (GridViewRow dr in GridView1.Rows)
{
string str = "";
if (((RadioButton)dr.FindControl("RadioButton1")).Checked)
{
str = "A";
}
else if (((RadioButton)dr.FindControl("RadioButton2")).Checked)
{
str = "B";
}
else if (((RadioButton)dr.FindControl("RadioButton3")).Checked)
{
str = "C";
}
else if (((RadioButton)dr.FindControl("RadioButton4")).Checked)
{
str = "D";
}
} --------------------编程问答-------------------- 重新生成一下试试,然后打上断点看是否能运行到断点! --------------------编程问答-------------------- 回帖是一种美德!每天回帖即可获得 10 分可用分 --------------------编程问答-------------------- 可以运行到断点,但是即使选中了,它也会跳到下一步! --------------------编程问答-------------------- 试试
if (((RadioButton)dr[列号].FindControl("RadioButton1")).Checked)
--------------------编程问答-------------------- 无反应是什么意思???STR没值还是什么? --------------------编程问答-------------------- 回帖是一种美德!每天回帖即可获得 10 分可用分 --------------------编程问答--------------------
if (((RadioButton)dr.FindControl("RadioButton1")).Checked)
{
str = "A";
Console.WriteLine(str);
}
输出来看看有没有值 --------------------编程问答-------------------- 前台代码是什么?
<asp:RadioButton id=RadioButton1 runat="server" Text='<%# Eval("AnswerA") %>' GroupName="Sl"> </asp:RadioButton>
<asp:RadioButton id=RadioButton2 runat="server" Text='<%# Eval("AnswerB") %>' GroupName="Sl">
</asp:RadioButton>
<asp:RadioButton id=RadioButton3 runat="server" Text='<%# Eval("AnswerC") %>' GroupName="Sl"> </asp:RadioButton>
<asp:RadioButton id=RadioButton4 runat="server" Text='<%# Eval("AnswerD") %>' GroupName="Sl"> </asp:RadioButton>
--------------------编程问答-------------------- 就是的说,即使RadioButton1被选中了也不会输出A --------------------编程问答-------------------- 前台代码就是这个!
<asp:RadioButton id=RadioButton1 runat="server" Text=' <%# Eval("AnswerA") %>' GroupName="Sl"> </asp:RadioButton>
<asp:RadioButton id=RadioButton2 runat="server" Text=' <%# Eval("AnswerB") %>' GroupName="Sl">
</asp:RadioButton>
<asp:RadioButton id=RadioButton3 runat="server" Text=' <%# Eval("AnswerC") %>' GroupName="Sl"> </asp:RadioButton>
<asp:RadioButton id=RadioButton4 runat="server" Text=' <%# Eval("AnswerD") %>' GroupName="Sl"> </asp:RadioButton> --------------------编程问答-------------------- 自己顶! --------------------编程问答--------------------
如果你同时选中几个,只能输出最后那一个!!!要么你每个里面都放一个输出!!!不知道理解对了没? --------------------编程问答-------------------- 你说的意思我明白,可能你没明白我的意思,现在我的程序是不管你选中几个,他都不会输出 --------------------编程问答-------------------- 可能你代码哪里有问题,这是个测试的例子,可以输出选择了哪个
前台页:
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table id="Table2" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
<tr>
<td colspan="3">
<asp:Label ID="Label1" runat="server" Text='<%# Container.DataItemIndex+1 %>'>
</asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Title","、{0}") %>'>
</asp:Label>
</td>
</tr>
<tr>
<td width="35%">
<asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("AnswerA") %>' GroupName="Sl">
</asp:RadioButton></td>
<td width="35%">
<asp:RadioButton ID="RadioButton2" runat="server" Text='<%# Eval("AnswerB") %>' GroupName="Sl">
</asp:RadioButton></td>
<td>
</td>
</tr>
<tr>
<td width="35%">
<asp:RadioButton ID="RadioButton3" runat="server" Text='<%# Eval("AnswerC") %>' GroupName="Sl">
</asp:RadioButton></td>
<td width="35%">
<asp:RadioButton ID="RadioButton4" runat="server" Text='<%# Eval("AnswerD") %>' GroupName="Sl">
</asp:RadioButton></td>
<td>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Font-Size="12pt" HorizontalAlign="Left" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="点击测试" />
后台页:
--------------------编程问答--------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = DataTablebind1();
GridView1.DataBind();
}
}
DataTable DataTablebind1()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
dt.Columns.Add(new DataColumn("title", typeof(String)));
dt.Columns.Add(new DataColumn("AnswerA", typeof(String)));
dt.Columns.Add(new DataColumn("AnswerB", typeof(String)));
dt.Columns.Add(new DataColumn("AnswerC", typeof(String)));
dt.Columns.Add(new DataColumn("AnswerD", typeof(String)));
for (int i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "题目" + i;
dr[2] = "A";
dr[3] = "B";
dr[4] = "C";
dr[5] = "D";
dt.Rows.Add(dr);
}
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow dr in GridView1.Rows)
{
string str = "";
if (((RadioButton)dr.FindControl("RadioButton1")).Checked)
{
str = "A";
}
else if (((RadioButton)dr.FindControl("RadioButton2")).Checked)
{
str = "B";
}
else if (((RadioButton)dr.FindControl("RadioButton3")).Checked)
{
str = "C";
}
else if (((RadioButton)dr.FindControl("RadioButton4")).Checked)
{
str = "D";
}
Response.Write(str + "<br />"); //测试选择的值
}
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string str = "";
if (((RadioButton)GridView1.Rows[i].FindControl("RadioButton1")).Checked)
{
str = "A";
}
else if (((RadioButton)GridView1.Rows[i].FindControl("RadioButton2")).Checked)
{
str = "B";
}
else if (((RadioButton)GridView1.Rows[i].FindControl("RadioButton3")).Checked)
{
str = "C";
}
else if (((RadioButton)GridView1.Rows[i].FindControl("RadioButton4")).Checked)
{
str = "D";
}
Response.Write(str);
}
测试通过 --------------------编程问答--------------------
foreach ( GridViewRow gv in this.GridView1.Rows )
{
string str = "";
if (((RadioButton)gv.FindControl("RadioButton1")).Checked)
{
str = "A";
}
else if (((RadioButton)gv.FindControl("RadioButton2")).Checked)
{
str = "B";
}
else if (((RadioButton)gv.FindControl("RadioButton3")).Checked)
{
str = "C";
}
else if (((RadioButton)gv.FindControl("RadioButton4")).Checked)
{
str = "D";
}
Response.Write(str);
}
也没问题啊。都可以取到str的值 --------------------编程问答-------------------- foreach (GridViewRow gr in GridView1.Rows)
{
if(((RadioButton)gr.FindControl("RadioButton1")).Checked)
{
}
}
RadioButton rb = (RadioButton)sender;
GridViewRow row = (GridViewRow)rb.NamingContainer;
((RadioButton)row.FindControl("RadioButton1")).Checked = true;
删除GroupName="Sl"看看,还不行就是代码问题
补充:.NET技术 , ASP.NET