How to retrieve data from a dynamically generated radio button

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 13
Reputation: Aswathy is an unknown quantity at this point 
Solved Threads: 0
Aswathy Aswathy is offline Offline
Newbie Poster

How to retrieve data from a dynamically generated radio button

 
0
  #1
Feb 1st, 2007
Hi ,

I have been trying to create an online exam application and is facing some issue with dynamically generated radio buttons.

My problem is discussed below:

I have a table with a set of questions.I dispaly the questions on the screen with a set of dynamically generated radio buttons with the answer options . I also have a submit button.

On the click of the submit button i need all the selected answers to be stored in a table in db.

I am not able to retrieve the data from the selected radio button.
I have been trying out this since y'day and unfortunately couldn't solve this issue.

Could somebody help me out in solving this problem of mine.

Regards,
Aswathy
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

Re: How to retrieve data from a dynamically generated radio button

 
0
  #2
Feb 1st, 2007
I'd load the radio buttons into a panel or place holder, then in the code for the submit button I'd do something like
if(phRadioButtons.HasControls())
{
foreach(Control c in control.Controls)
{
Type type = c.GetType();
if(type.FullName == "System.Web.UI.WebControls.RadioButton")

then you can add code to read the value and add to the db
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How to retrieve data from a dynamically generated radio button

 
0
  #3
Feb 1st, 2007
If you post some code we can help, hard to know what your issue is. Typically most people get stuck with dynamically created controls because they don't understand the page life cycle in ASP.NET. Are you using individual RadiButtons or a RadioButtonList server control or HTML input radio buttons? are they created in a datagrid template column or what? the best way to store/get the values differs depending on your implementation.

heres an example I've posted before in these forums. Its developed against the .NET 2.0 framework, you don't say which you are developing for:

ASPX
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Radiolist.aspx.cs" Inherits="Radiolist" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>Untitled Page</title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
  13. <Columns>
  14. <asp:TemplateField HeaderText="Select">
  15. <ItemTemplate>
  16. <input type="radio" name="radiolist" value="<%# Container.DataItem%>" />
  17. </ItemTemplate>
  18. </asp:TemplateField>
  19. <asp:TemplateField HeaderText="Data">
  20. <ItemTemplate>
  21. <%# Container.DataItem %>
  22. </ItemTemplate>
  23. </asp:TemplateField>
  24. </Columns>
  25. </asp:GridView>
  26. <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
  27. <asp:Label ID="lblSelected" runat="server"></asp:Label></div>
  28. </form>
  29. </body>
  30. </html>

ASPX.CS
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class Radiolist : System.Web.UI.Page
  13. {
  14. String[] data = new String[5];
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. for (int i = 0; i < 5; i++)
  18. {
  19. data[i] = i.ToString();
  20. }
  21.  
  22. GridView1.DataSource = data;
  23. GridView1.DataBind();
  24.  
  25. }
  26. protected void Button1_Click(object sender, EventArgs e)
  27. {
  28. if (Request.Form["radiolist"] == null)
  29. lblSelected.Text = "You didn't choose anything";
  30. else
  31. lblSelected.Text = "You choose " + Request.Form["radiolist"];
  32. }
  33. }
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 13
Reputation: Aswathy is an unknown quantity at this point 
Solved Threads: 0
Aswathy Aswathy is offline Offline
Newbie Poster

Solved How to retrieve data from a dynamically generated radio button

 
0
  #4
Feb 5th, 2007
Originally Posted by sedgey View Post
I'd load the radio buttons into a panel or place holder, then in the code for the submit button I'd do something like
if(phRadioButtons.HasControls())
{
foreach(Control c in control.Controls)
{
Type type = c.GetType();
if(type.FullName == "System.Web.UI.WebControls.RadioButton")

then you can add code to read the value and add to the db
Thanks,

That's working ....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC