Mutual exclusive selection of Radiobutton in gridView.ASP.NET C #

<asp:TemplateField HeaderText="Select One"> <ItemTemplate> <asp:RadioButton ID="RadioButton1" runat="server" /> </ItemTemplate> </asp:TemplateField> 

aspx.cs

 protected void Button1_Click(object sender, EventArgs e) { foreach (GridViewRow di in GridView1.Rows) { RadioButton rad = (RadioButton)di.FindControl("RadioButton1"); if (rad.Checked&&rad!=null) { s = di.Cells[1].Text; } } Response.Redirect("applicants.aspx?form=" +s); } 

I select the rows that are selected with this, but I have a problem. I want the user to be able to select only one radiobutton , but he allowed to immediately select all the radiobutton . Can you help me remove this problem please. you are welcome. enter image description here

+4
source share
5 answers

Maybe I'm too late at the party, but it will be a trick, check here

This may be useful for those who are looking at this answer in the future.

+3
source

On the ASP page, you need to set the GroupName property to the same for all radio buttons, for example:

 <asp:RadioButton ID="RadioButton1" runat="server" GroupName="RadioGroup" /> 
+1
source

well for this you can use follwing code first you must define groupName.The follwing code will work

  <asp:RadioButton ID="RadioButton1" OnCheckedChanged="rbSelector_CheckedChanged" AutoPostBack="true" GroupName="Apply" runat="server"></asp:RadioButton> 

WITH#

 protected void rbSelector_CheckedChanged(object sender, System.EventArgs e) { foreach (GridViewRow oldrow in GridView2.Rows) { ((RadioButton)oldrow.FindControl("RadioButton1")).Checked = false; } //Set the new selected row RadioButton rb = (RadioButton)sender; GridViewRow row = (GridViewRow)rb.NamingContainer; ((RadioButton)row.FindControl("RadioButton1")).Checked = true; } 
0
source
0
source

Using the GroupName property alone will not work, each switch will still receive a unique attribute for the name, since it is located on different grid lines.

One option is to manually break the switch markup using the Literal control ( example ). This will make it possible to easily group radio buttons on the client side, but it requires a bit more work to determine which button was selected during postback.

When I needed this behavior, it was easier for me to keep the radio buttons as server-side controls and just apply the w / jQuery button group. Put the RadioButton in the TemplateField, as you showed, then add this code to uncheck all the other buttons if one of them is checked:

 $(document).ready(function () { // could also pass in a unique ID selector createManualRadioButtonGroupForGridView(".myGridViewClass"); }); function createManualRadioButtonGroupForGridView(gridViewSelector) { $(gridViewSelector + " input[type=radio]").change(function () { var checkedRadioButton = this; $(gridViewSelector + " input[type=radio]").each(function (e) { if (this !== checkedRadioButton) { $(this).prop("checked", false); } }); }); } 
0
source

All Articles