Hi,

I am using below code to check only one radio button which is in gridview item template. But it allow multiple selection as CheckBoxes.

My code:

<columns>
<asp:TemplateField HeaderText="Select">
                                <ItemTemplate >
                                    <asp:RadioButton ID="rd1" GroupName="rdBox"  runat="server"  />
                                </ItemTemplate>
                               </asp:TemplateField>
</columns>

Please help me.

Thanks !

Pankaj Singh

Recommended Answers

All 6 Replies

You question is not clear. Please be precise.

To check multiple check boxes you must have it at first. But i don't see it in your code. If you try to check multiple check boxes with the same GroupName from codebehind it will allow you to do so but if user tries to select multiple checkboxes with the same group name in the web page it will just not happen.

Try this. The code is tested.

<asp:TemplateField HeaderText="Select">
<ItemStyle HorizontalAlign="Center" Width="15%" />
<HeaderStyle HorizontalAlign="Center" Font-Size="13px" Font-Bold="false" />
<ItemTemplate>
<asp:RadioButton ID="rbtnSelect" AutoPostBack="true" runat="server" OnCheckedChanged="rbtnSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>



*************************************************************



protected void rbtnSelect_CheckedChanged(object sender, EventArgs e)
{
RadioButton selectButton = (RadioButton)sender;
GridViewRow row = (GridViewRow)selectButton.Parent.Parent;
int a = row.RowIndex;
foreach (GridViewRow rw in gvwMixes.Rows)
{
if (selectButton.Checked)
{
if (rw.RowIndex != a)
{
RadioButton rd = rw.FindControl("rbtnSelect") as RadioButton;
rd.Checked = false;
}
}
}
}

First give the groupname to radio button so that All radio buttons in rows would have same group name. I hope this will word, if not, try to do it through javascript, you don't need to write server side code if you are not applying pagination in your grid.

I have use this its working fine with autopostback="true"

Single radio button selection can be done in two ways.
1) Client side validation using JavaScript and JQuery.
2) Validating from Code behind.

Validate from Code Behind.

<Columns>
    <asp:TemplateField>
         <ItemTemplate>
             <asp:RadioButton runat="server" id="rb" />
         </ItemTemplate>
    </asp:TemplateField>
</Columns>

For index As Integer = 0 To GridView1.Rows.Count - 1
    ' ACCESS THE RADIO BUTTONS FROM THE TEMPLATE FIELD.
    Dim rb As RadioButton = CType(GridView1.Rows(index).FindControl("rb"), RadioButton)

    If rb.Checked Then
       ... write your code.
    End If
Next

For validating from Client see this link Check if Any GridView Row With RadioButton is Selected Using JQuery.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.