Hi setwell,
You need to use Repeater OnItemDataBound event handler to populate the RadioButtonList.
Please refer the below example.
Database
CREATE TABLE QuestionTable(QuestionId INT NOT NULL,QuestionDescription NVARCHAR(500) NOT NULL)
INSERT INTO QuestionTable VALUES(1,'Who is the Prime minister of India')
INSERT INTO QuestionTable VALUES(2,'Capital City of Maharashtra')
INSERT INTO QuestionTable VALUES(3,'Maximum run in ODI by a batsman')
CREATE TABLE OptionTable(QuestionId INT NOT NULL,Options NVARCHAR(500) NOT NULL)
INSERT INTO OptionTable VALUES(1,'LK Advani')
INSERT INTO OptionTable VALUES(1,'Manmohan singh')
INSERT INTO OptionTable VALUES(1,'Sachin Tendulker')
INSERT INTO OptionTable VALUES(1,'N. Modi')
INSERT INTO OptionTable VALUES(2,'Delhi')
INSERT INTO OptionTable VALUES(2,'Mumbai')
INSERT INTO OptionTable VALUES(2,'Kolkata')
INSERT INTO OptionTable VALUES(3,'Ricky Pointing')
INSERT INTO OptionTable VALUES(3,'Sachin Tendulker')
HTML
<asp:Repeater ID="rptQNA" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table class="table">
<tr>
<th scope="col" style="width: auto">Id </th>
<th scope="col" style="width: auto">Question </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblQuestionId" runat="server" Text='<%# Eval("QUESTIONID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("QUESTIONDESCRIPTION") %>'></asp:Label>
<br />
<asp:RadioButtonList ID="rblOptions" runat="server" BorderStyle="None">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindQuestionRepeater();
}
}
private void BindQuestionRepeater()
{
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT QUESTIONID, QUESTIONDESCRIPTION FROM QuestionTable";
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter oda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
oda.Fill(dt);
rptQNA.DataSource = dt;
rptQNA.DataBind();
}
}
}
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string questionId = (e.Item.FindControl("lblQuestionId") as Label).Text;
RadioButtonList rblOptions = e.Item.FindControl("rblOptions") as RadioButtonList;
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT QUESTIONID, OPTIONS FROM OPTIONTABLE WHERE QUESTIONID = @QuestionId";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("QuestionId", questionId);
cmd.Connection = con;
con.Open();
rblOptions.DataSource = cmd.ExecuteReader();
rblOptions.DataTextField = "OPTIONS";
rblOptions.DataValueField = "QUESTIONID";
rblOptions.DataBind();
con.Close();
}
}
}
}
VB.Net
Private conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindQuestionRepeater()
End If
End Sub
Private Sub BindQuestionRepeater()
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT QUESTIONID, QUESTIONDESCRIPTION FROM QuestionTable"
Using cmd As SqlCommand = New SqlCommand(query, con)
Using oda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
oda.Fill(dt)
rptQNA.DataSource = dt
rptQNA.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim questionId As String = (TryCast(e.Item.FindControl("lblQuestionId"), Label)).Text
Dim rblOptions As RadioButtonList = TryCast(e.Item.FindControl("rblOptions"), RadioButtonList)
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT QUESTIONID, OPTIONS FROM OPTIONTABLE WHERE QUESTIONID = @QuestionId"
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("QuestionId", questionId)
cmd.Connection = con
con.Open()
rblOptions.DataSource = cmd.ExecuteReader()
rblOptions.DataTextField = "OPTIONS"
rblOptions.DataValueField = "QUESTIONID"
rblOptions.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Screenshot