-1

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

6
Contributors
6
Replies
26
Views
8 Years
Discussion Span
Last Post by arun1123
0

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.

0

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

Edited by Nick Evan: Fixed formatting

0

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.

0

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.

Edited by arun1123

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.