How to use switches in grid mode in asp.net

How to implement switches in the form of a grid? I used the button asp:radiob, but the problem is that it selects all the switches in the list. How to choose only one switch at a time?

+3
source share
4 answers

Make all the radio buttons part of the group by highlighting them GroupName.

Here is an example:

<html>
<body>

<form runat="server">
Select your favorite color:
<br />
<asp:RadioButton id="red" Text="Red" Checked="True" 
GroupName="colors" runat="server"/>
<br />
<asp:RadioButton id="green" Text="Green"
GroupName="colors" runat="server"/>
<br />
<asp:RadioButton id="blue" Text="Blue" 
GroupName="colors" runat="server"/>
<br />
<asp:Button text="Submit" OnClick="submit" runat="server"/>
<p><asp:Label id="Label1" runat="server"/></p>
</form>

</body>
</html>
0
source

You can add switches to the gridview using TemplateField.

<Columns>  
  <asp:TemplateField>  
    <ItemTemplate>  
      <asp:RadioButton ID="rdoYes" runat="server" Text="Yes" Checked="true" />  
    </ItemTemplate>  
   </asp:TemplateField>  
</Columns>  

You can choose individual radio buttonif you have added GridViewas described above.

+2
source

, TemplateField, , rbSelector_CheckedChanged()...

protected void rbSelector_CheckedChanged (object sender, System.EventArgs e)

{

   //Clear the existing selected row 
   foreach (GridViewRow oldrow in GridView1.Rows)
   {
       ((RadioButton)oldrow.FindControl("rbSelector")).Checked = false;
   }

   //Set the new selected row
   RadioButton rb = (RadioButton)sender;
   GridViewRow row = (GridViewRow)rb.NamingContainer;
   ((RadioButton)row.FindControl("rbSelector")).Checked = true;

}

If a problem arises, just let me know, okay? Hope this code can help newbies like me.

Amit Patel

0
source

Use TemplateFieldwith standard HTML control and then use on codebehind Request.Form.

Aspx:

<asp:TemplateField>
    <ItemTemplate>
        <input type="radio" name="group1" value='<%# Eval("YourValue") %>' />
    </ItemTemplate>
</asp:TemplateField>

Codebehind:

string radioValue = Request.Form["group1"].ToString();
0
source

Source: https://habr.com/ru/post/1752760/


All Articles