Solved

View State of Radio Button in GridView

Posted on 2008-06-24
4
1,912 Views
Last Modified: 2013-11-26
How can I maintain state of an html radio button within a <asp:gridview>?
<asp:GridView>
<columns>
<asp:TemplateField>
  <ItemTemplate>
     <input type="radio" name="rbSelect" id="rbSelect" value="<%# Eval("first_name")%> <%# Eval("last_name")%>" />
   </ItemTemplate>
</asp:TemplateField>
0
Comment
Question by:kevbob650
  • 3
4 Comments
 
LVL 2

Expert Comment

by:Alexey_Varlamov
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";
        else
            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
}
0
 

Author Comment

by:kevbob650
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" />

            

            <Columns> 

                

                <asp:TemplateField>

                    <ItemTemplate>

                        <input type="radio" id="rbSelect" runat="server" value='<%#Eval("first_name") %>' />

                    </ItemTemplate>

                </asp:TemplateField>

               

                <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" />                

            

            </Columns>

        </asp:GridView>

Open in new window

0
 
LVL 2

Expert Comment

by:Alexey_Varlamov
ID: 21867998
The explanation and possible solution for RadioButton grouping issue:
http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx

and possible AutoPostback issue of this solution:
http://www.codeproject.com/KB/webforms/groupradiobuttons.aspx

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

Accepted Solution

by:
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
}
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now