开发者

Multiple radiobutton columns in gridview

开发者 https://www.devze.com 2023-01-10 19:35 出处:网络
I have multiple radio buttons columns in my gridview, I want t开发者_如何学JAVAo select one column at a time As i got, you want to check only one radio button in a row so just add an attribute

I have multiple radio buttons columns in my gridview, I want t开发者_如何学JAVAo select one column at a time


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号