View State of Radio Button in GridView

Posted on 2008-06-24
Last Modified: 2013-11-26
How can I maintain state of an html radio button within a <asp:gridview>?
     <input type="radio" name="rbSelect" id="rbSelect" value="<%# Eval("first_name")%> <%# Eval("last_name")%>" />
Question by:kevbob650
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Expert Comment

ID: 21860252
1. What kind of state do you want to monitor? The ViewState available in server controls only, so you should use runat="server" within <input type="radio" ..> tags

2. To catch the POST value (even ViewState not used)  you can use a code like this:
  if (Request.Form["rbSelect"] == null)
            lblSelected.Text = "You didn't choose anything";
            lblSelected.Text = "You choose " + Request.Form["rbSelect"];

3. To find the RadioButton objects in all rows use can use a code like this:
foreach(GridViewRow row in GridView1.Rows)
  HtmlInputRadioButton radio1=row.FindControl("rbSelect") as HtmlInputRadioButton;
  // do something with radio1

Author Comment

ID: 21861237
When I add the runat server line in the <input type="radio"> tag, I am able to select more than one button and I'm not able to access the value?? When I remove the runat=server line, the radio button works, I'm able to retrieve the value, but it disapperars when the form is submitted.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource2" CellPadding="6" BorderWidth="1px" BorderStyle="Solid" BorderColor="Black" style="z-index: 109; left: 15px; position: absolute; top: 343px" Font-Names="Arial" Font-Size="10pt">
            <HeaderStyle ForeColor="Black" Font-Names="Arial" Font-Size="9pt" Font-Bold="True" BackColor="#66CCCC" HorizontalAlign="Left" />
            <RowStyle ForeColor="Black" BackColor="#CCFFCC" />
            <AlternatingRowStyle ForeColor="Black" BackColor="#CCFFFF" />
            <SelectedRowStyle ForeColor="White" BackColor="#009966" />
                        <input type="radio" id="rbSelect" runat="server" value='<%#Eval("first_name") %>' />
                <asp:BoundField DataField="first_name" HeaderText="First Name" SortExpression="first_name" />
                <asp:BoundField DataField="last_name" HeaderText="Last Name" SortExpression="last_name" />
                <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
                <asp:BoundField DataField="office_phone" HeaderText="Bus Phone" SortExpression="office_phone" />
                <asp:BoundField DataField="mobile_phone" HeaderText="Cell Phone" SortExpression="mobile_phone" />
                <asp:BoundField DataField="esc_level" HeaderText="Esc Level" SortExpression="esc_level" />                

Open in new window


Expert Comment

ID: 21867998
The explanation and possible solution for RadioButton grouping issue:

and possible AutoPostback issue of this solution:

/p.s. also, if you know the selected radio button (from previous postback), you can set the "Selected" property before rendering... i think... : )

Accepted Solution

Alexey_Varlamov earned 50 total points
ID: 21868054
... and why you are not able to access the value?  the standard way is a test the "Checked" property and get value if the Checked = true:

foreach(GridViewRow row in GridView1.Rows)
  HtmlInputRadioButton radio1=row.FindControl("rbSelect") as HtmlInputRadioButton;
  if (radio1.Checked)
     value = radio1.Value;
  // do something with radio1

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question