簡體   English   中英

在ASP.NET中的GridView中使用RadioButton的問題

[英]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

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選擇的行。 你能幫我解決這個問題嗎?

您只能將FindControl與服務器端控件一起使用。 用ASP.NET單選按鈕替換<input> HTML元素,例如:

<asp:RadioButton ID="MyRadioButton" runat="server"  ... />

你必須使用runat="server"

<input name="MyRadioButton" type="radio" runat="server" id="MyRadioButton" />

如前所述,添加runat =“server”並將if (rad.Checked&&rad!=null)計算的條件順序更改為if (rad!=null && rad.Checked)

順便說一句,在GridView專欄中制作單選按鈕並不容易。 如果您偶然發現問題,請查看此鏈接: 添加單選按鈕的GridView列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM