Search code examples
asp.netvb.netsql-server-2012radiobuttonlistdynamic-controls

How to Create Dynamic RadioButtonList with values from the DataBase


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.


Solution

  • 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