[英]Problem with usage of RadioButton in GridView in ASP.NET
<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<input name="MyRadioButton" type="radio" />
</ItemTemplate>
</asp:TemplateField>
aspx.cs 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);
}
I couldn't get the row which is selected in RadioButton
. 我无法获得在RadioButton
选择的行。 Can you help me with this please. 你能帮我解决这个问题吗?
You can only use FindControl
with server-side controls. 您只能将FindControl
与服务器端控件一起使用。 Replace your <input>
HTML element with an ASP.NET radio button, eg: 用ASP.NET单选按钮替换<input>
HTML元素,例如:
<asp:RadioButton ID="MyRadioButton" runat="server" ... />
你必须使用runat="server"
<input name="MyRadioButton" type="radio" runat="server" id="MyRadioButton" />
As already mentioned, add runat="server" and change order of conditions evaluated from if (rad.Checked&&rad!=null)
to if (rad!=null && rad.Checked)
如前所述,添加runat =“server”并将if (rad.Checked&&rad!=null)
计算的条件顺序更改为if (rad!=null && rad.Checked)
By the way it's not so easy to make radiobuttons in GridView column exclusive. 顺便说一句,在GridView专栏中制作单选按钮并不容易。 Look at this link if you will stumble on problem: Adding a GridView Column of Radio Buttons 如果您偶然发现问题,请查看此链接: 添加单选按钮的GridView列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.