Amit Dhall
Amit Dhall

Reputation: 1315

Multiple radiobutton columns in gridview

I have multiple radio buttons columns in my gridview, I want to select one column at a time

Upvotes: 0

Views: 3415

Answers (2)

CountMurphy
CountMurphy

Reputation: 1096

This rather lengthy tutorial is the best I've seen that solves this issue

Adding a GridView Column of Radio Buttons
http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-radio-buttons-cs

If you are using the same group name for your buttons and they still are not working:

"The reason the radio buttons are not grouped is because their rendered name attributes are different, despite having the same GroupName property setting." If you view the source it might look something like this:

<input id="ctl00_MainContent_Suppliers_ctl02_RowSelector" 
    name="ctl00$MainContent$Suppliers$ctl02$SuppliersGroup" 
    type="radio" value="RowSelector" />
<input id="ctl00_MainContent_Suppliers_ctl03_RowSelector" 
    name="ctl00$MainContent$Suppliers$ctl03$SuppliersGroup" 
    type="radio" value="RowSelector" />

The fix is to use literal controls to inject the name in.

protected void Suppliers_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Grab a reference to the Literal control
        Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
        // Output the markup except for the "checked" attribute
        output.Text = string.Format(
            @"<input type="radio" name="SuppliersGroup" " +
            @"id="RowSelector{0}" value="{0}" />", e.Row.RowIndex);
    }
}

Upvotes: 2

Azhar
Azhar

Reputation: 20670

As i got, you want to check only one radio button in a row so just add an attribute

GroupName with same value to all radio buttons and it will work..

e.g

    <asp:TemplateField HeaderText="More Than 20% Estimate"  >
 <ItemTemplate >
 <asp:RadioButton ID="rdbGVRow8" GroupName ="Program"  onclick="javascript:CheckOtherIsCheckedByGVIDMore(this);" runat="server" />
  </ItemTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="10% to 20% overestimate"  >
 <ItemTemplate >
 <asp:RadioButton ID="rdbGVRow7" GroupName ="Program" onclick="javascript:CheckOtherIsCheckedByGVIDMore(this);" runat="server" />
 </ItemTemplate>
 </asp:TemplateField>
    .
    .
    .
    .

where program is a value you can give your own value but remember same value to all radio buttons.

Upvotes: 4

Related Questions