471,123 Members | 791 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

GridView RadioButtonList Values

15
Hello friends please advise.......

I am building an application for Survey. For this i have GridView with two Template fields. The first Template has Label (To display question text) and RadioButtonList (for options). The 2nd Template field is the primary key value. And i have a button on the form.

In the button click event i wrote code to grab RadioButtonList values.But the selected value is coming as null....PLEASE ADVISE....

Let me know if my question is unclear!!!!!
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None">
  2.  
  3. <Columns>
  4.  
  5. <asp:TemplateField SortExpression="Question_Text">
  6.  
  7.     <EditItemTemplate>
  8.         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Question_Text")>'>/asp:TextBox>
  9.     </EditItemTemplate>
  10.  
  11.     <ItemTemplate>
  12.         <asp:Label ID="Label10" runat="server" Text='<%# Bind("Question_Text") %>'></asp:Label>
  13.         <asp:RadioButtonList ID="RadioButtonList1" Visible='<%# GetRadioSetting(Eval("Answer_Type")) %>' runat="server" RepeatColumns="5" RepeatDirection="Horizontal" RepeatLayout="Flow" Width="400px">
  14.             <asp:ListItem>N/A</asp:ListItem>
  15.             <asp:ListItem>1</asp:ListItem>
  16.             <asp:ListItem>2</asp:ListItem>
  17.             <asp:ListItem>3</asp:ListItem>
  18.             <asp:ListItem>4</asp:ListItem>
  19.         </asp:RadioButtonList>
  20.     </ItemTemplate>
  21.  
  22. </asp:TemplateField>
  23.  
  24. <asp:TemplateField SortExpression="Question_Number" Visible="False">
  25.     <EditItemTemplate>
  26.         <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Question_Number") %>'></asp:TextBox>
  27.     </EditItemTemplate>
  28.     <ItemTemplate>
  29.         <asp:Label ID="Label2" runat="server" Text='<%# Bind("Question_Number") %>'></asp:Label>
  30.     </ItemTemplate>
  31. </asp:TemplateField>
  32.  
  33. </Columns>
  34.  
  35. </asp:GridView>
  36.  
  37. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
  38.  
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3.     IDictionary<int, string> values = new Dictionary<int, string>();
  4.  
  5.     foreach (GridViewRow row in GridView1.Rows)
  6.     {
  7.         // Assuming Question_Number is placed into DataKeyNames property
  8.         int questionNumber = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
  9.  
  10.         RadioButtonList lst = row.FindControl("RadioButtonList1") as RadioButtonList;
  11.         string answer = (lst != null) ? lst.SelectedValue.ToString() : String.Empty;
  12.  
  13.         values.Add(questionNumber, answer);
  14.     }
  15.  
  16. }
  17.  
questionNumber value is coming correct.but answer="" for all the questions.

PLEASE ADVISE.....
Feb 10 '09 #1
1 10650
Frinavale
9,735 Expert Mod 8TB
Is your GridView in Edit Mode when the button is clicked?
If so, your RadioButtonList wont exist.

Are you, by any chance, binding the Data to the GridView during the Page Load event?
If so then you'll lose what the user has selected.
Feb 11 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Giovanni | last post: by
3 posts views Thread by cpnet | last post: by
1 post views Thread by Eric Effer | last post: by
5 posts views Thread by NKaufman | last post: by
1 post views Thread by l3d007 | last post: by
reply views Thread by =?Utf-8?B?QW5keSBpbiBTb3V0aCBKZXJzZXk=?= | last post: by
13 posts views Thread by SAL | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.