How to Create Dynamic RadioButtonList with values from the DataBase

13.1k Views Asked by At

My table structure

Group | Values
------  ------
Group1   Option 1
Group1   Option 2
Group1   Option 3
Group1   Option 4

Group2   Option 1
Group2   Option 2
Group2   Option 3
Group2   Option 4

Now with the above table values i need to create dynamic radiobuttonlist control like below

Group 1

  • Option 1
  • Option 2
  • Option 3
  • Option 4

Group 2

  • Option 1
  • Option 2
  • Option 3
  • Option 4

Is there any possibility to do this?

And the number of radiobutton list is also dynamic. It may have any number of radiobutton list. I need to give unique ID for each radiobuttonlist and get values from all the radiobuttonlist.

And this is similar to the Online Examination but it is not the same for your better understanding i have mentioned Online Examination.

3

There are 3 best solutions below

0
On BEST ANSWER
dtgrp = db.Getval(select distinct Group from table1)
dtval = db.GetVal(select Values from table1)

 For i = 0 To dtgrp.Columns.Count - 1
 Dim rdl As New RadioButtonList
 rdl.ID = dtgrp.Columns(i).ToString()
 rdl.Text = dtgrp.Columns(i).ToString() & vbCrLf
 For j = 0 To dtval.Rows.Count - 1
 If Not String.IsNullOrEmpty(dtval.Rows(j)(i).ToString()) Then
 rdl.Items.Add(dtval.Rows(j)(i).ToString())
 End If
 Next
 pnlgrp.Controls.Add(rdl)
 Next
1
On

THis will give your answer:

 <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder runat="server" ID="PlaceHolder1" />
        <asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="Submit" />
        <asp:Label runat="server" ID="Label1" />
    </div>
    </form>

 protected void Button1_Click(object sender, EventArgs e)
        {
            LoadControls();
        }
        private void LoadControls()
        {


            string conString = ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from groupInfo order by group1", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            string strGroupName = string.Empty;
            DataTable dt = ds.Tables[0];
            int flag = 0;

            RadioButtonList rblGroup = new RadioButtonList();
            Label lblGroup;
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                while (strGroupName == Convert.ToString(dt.Rows[i][0]))
                {
                    i++;
                    goto Outer;
                }

                strGroupName = Convert.ToString(dt.Rows[i][0]);
                rblGroup = new RadioButtonList();
                lblGroup = new Label();
                lblGroup.Text = strGroupName;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (strGroupName == Convert.ToString(dt.Rows[j][0]))
                    {
                        ListItem lblItem = new ListItem(Convert.ToString(dt.Rows[j][1]), Convert.ToString(dt.Rows[j][1]));
                        rblGroup.Items.Add(lblItem);
                    }
                }
                PlaceHolder1.Controls.Add(lblGroup);
                PlaceHolder1.Controls.Add(rblGroup);

            Outer:
                continue;
            }
        }
2
On

Basically, if you create a control dynamically, you will need to reload those controls (with same id) in every post back of the page.

Otherwise, they will become null, and you won't be able to access them.

Here is a sample. It loads RadioButtonList control dynamically and displays the selected value back when a button is clicked.

<asp:PlaceHolder runat="server" ID="PlaceHolder1"/>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="Submit" />
<asp:Label runat="server" ID="Label1"/>

protected void Page_Load(object sender, EventArgs e)
{
    LoadControls();
}

protected void Button1_Click(object sender, EventArgs e)
{
    var radioButtonList = PlaceHolder1.FindControl("1") as RadioButtonList;
    Label1.Text = radioButtonList.SelectedValue;
}

private void LoadControls()
{
    var tmpRBL = new RadioButtonList();
    tmpRBL.ID = "1";

    for (int i = 1; i <= 5; i++)
    {
        var tmpItem = new ListItem(i.ToString(), i.ToString());
        tmpRBL.Items.Add(tmpItem);
    }

    PlaceHolder1.Controls.Add(tmpRBL);
}