<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<input name="MyRadioButton" type="radio" />
</ItemTemplate>
</asp:TemplateField>
aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow di in GridView1.Rows)
{
RadioButton rad = (RadioButton)di.FindControl("MyRadioButton");
//Giving Error:Object reference not set to an instance of an object.
if (rad.Checked&&rad!=null)
{
s = di.Cells[1].Text;
}
}
Response.Redirect("applicants.aspx?form=" +s);
}
我无法获得RadioButton
中选中的行。你能帮我解决这个问题。
答案 0 :(得分:1)
您只能将FindControl
与服务器端控件一起使用。将您的<input>
HTML元素替换为ASP.NET单选按钮,例如:
<asp:RadioButton ID="MyRadioButton" runat="server" ... />
答案 1 :(得分:1)
你必须使用runat="server"
<input name="MyRadioButton" type="radio" runat="server" id="MyRadioButton" />
答案 2 :(得分:0)
如前所述,添加runat =“server”并将评估的条件顺序从if (rad.Checked&&rad!=null)
更改为if (rad!=null && rad.Checked)
顺便说一句,在GridView专栏中制作单选按钮并不容易。如果您偶然发现问题,请查看此链接:Adding a GridView Column of Radio Buttons