Renu123
Renu123

Reputation: 1363

how to use radio buttons in grid view in asp.net

How do I implement radio buttons in a grid view? I used asp:radiob button but the problem is that it selects all the radio buttons in the list. How do I select only one radio button at a time?

Upvotes: 3

Views: 13462

Answers (4)

SelvaS
SelvaS

Reputation: 2125

You can add the radio buttons in GridView using TemplateField.

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

You can select individual radio button if you added in GridView like above.

Upvotes: 2

Richard YS
Richard YS

Reputation: 1442

Use a TemplateField with a standard HTML control then on the codebehind use Request.Form.

ASPX:

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

Codebehind:

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

Upvotes: 0

Amit Patel
Amit Patel

Reputation: 76

If your going to use a grid view and u want to put a radio button on a TemplateField to act as a pointer to your selection just use this code on the 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 theres any problem just let me know, ok? hope this code can help newbies out there like me.

Amit Patel

Upvotes: 0

VoodooChild
VoodooChild

Reputation: 9784

Make all the radio buttons part of a group by defing a GroupName for them.

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>

Upvotes: 0

Related Questions