Naresh
Naresh

Reputation: 657

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);

}

I couldn't get the row which is selected in RadioButton. Can you help me with this please.

Upvotes: 0

Views: 3690

Answers (3)

IUnknown
IUnknown

Reputation: 22448

As already mentioned, add runat="server" and change order of conditions evaluated from if (rad.Checked&&rad!=null) to if (rad!=null && rad.Checked)

By the way it's not so easy to make radiobuttons in GridView column exclusive. Look at this link if you will stumble on problem: Adding a GridView Column of Radio Buttons

Upvotes: 0

Mahesh KP
Mahesh KP

Reputation: 6446

you have to use runat="server"

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

Upvotes: 1

M4N
M4N

Reputation: 96551

You can only use FindControl with server-side controls. Replace your <input> HTML element with an ASP.NET radio button, e.g:

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

Upvotes: 1

Related Questions