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

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

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Radiolist.aspx.cs" Inherits="Radiolist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <input type="radio" name="radiolist" value="<%# Container.DataItem%>" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Data">
                    <ItemTemplate>
                        <%# Container.DataItem %>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:Label ID="lblSelected" runat="server"></asp:Label></div>
    </form>
</body>
</html>

ASPX.CS

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Radiolist : System.Web.UI.Page
{
    String[] data = new String[5];
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 5; i++)
        {
            data[i] = i.ToString();
        }

        GridView1.DataSource = data;
        GridView1.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.Form["radiolist"] == null)
            lblSelected.Text = "You didn't choose anything";
        else
            lblSelected.Text = "You choose " + Request.Form["radiolist"];
    }
}

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 ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.