tags:

views:

185

answers:

4

Hi, I have a gridview, and I put a radio column for it. But radio ids are not unique. I've read http://www.asp.net/Learn/data-access/tutorial-51-cs.aspx http://shawpnendu.blogspot.com/2009/02/problem-to-group-radio-button-across.html http://aspnet.4guysfromrolla.com/articles/122602-1.aspx but they are complicated. Do you have other solution, with c# code behind? I've also tried to give unique id with a function from code behind, but ID='<%# function(...) %>' is not allowed. Thank you in advance

A: 

Not totally sure what your question is, but here goes.

If you need to find a RadioButton in a GridViewRow, first get the row, then use FindControl()

GridViewRow myRow = GridView1.Rows[index];
RadioButton myRadioButton = (RadioButton)myRow.FindControl("nameOfRadioButton")

You can do that in reverse (if you need to handle the event RadioButton1_CheckedChanged, for example) by first casting the object sender to RadioButton, then casting RadioButton.Parent.Parent to GridViewRow, from which you can get the index.

Question:

Is the column you added a Template Column, in which you places a RadioButton? My two solutions will only work in this case.

Matthew Jones
Thanks, your answer learned me. However in my web, mutiple radiobuttons can be selected. I think it is because they have same ID
Ehsan
A: 

The radio buttons need to have the same name attribute. Not unique. This is how the browser knows how to group them.

Josh Stodola
it is runat server. but multiple selections are allowed.thanks for answer
Ehsan
I see. with this code multiple radios can be selected
Ehsan
A: 

there is gridview code:

<asp:GridView ID="gridView_stLists" runat="server" AutoGenerateColumns="False" CellPadding="3"
                                                    BorderStyle="NotSet" CssClass="table_layout" Width="500">
                                                    <RowStyle CssClass="table_body" />
                                                    <Columns>
                                                        <asp:TemplateField HeaderStyle-Width="20">
                                                            <ItemTemplate>
                                                                <asp:RadioButton ID="rdBtn_stdl" runat="server" OnCheckedChanged="rdBtn_stdl_CheckedChanged"
                                                                    AutoPostBack="True" GroupName="stdl" value='<%# Eval("uri") %>' />
                                                            </ItemTemplate>

I think radios must have different IDs in ID="rdBtn_stdl" section

Ehsan
A: 

Found a good method at:
http://www.asp.net/Learn/data-access/tutorial-51-cs.aspx

Ehsan