Hi ramco1917,
Please refer below sample.
HTML
<asp:Repeater ID="rptQuestionsEmp" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table >
<tr>
<th>#</th>
<th>Question</th>
<th>Options</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>Question
<asp:Label ID="lblQuestionId" runat="server" Text='<%# Eval("QuestionId") %>' />:</td>
<td>
<asp:HiddenField ID="hfQuestionID" runat="server" Value='<%#Eval("QuestionId")%>' />
<asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>' /></td>
</td>
<td>
<asp:Repeater ID="rptAnswersEmp" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton ID="rbtnOpt1" runat="server" Text='<%# Eval("Option1") %>' GroupName="G1" /><br />
<asp:RadioButton ID="rbtnOpt2" runat="server" Text='<%# Eval("Option2") %>' GroupName="G1" /><br />
<asp:RadioButton ID="rbtnOpt3" runat="server" Text='<%# Eval("Option3") %>' GroupName="G1" /><br />
<asp:RadioButton ID="rbtnOpt4" runat="server" Text='<%# Eval("Option4") %>' GroupName="G1" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save"/>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rptQuestionsEmp.DataSource = GetData("SELECT QuestionId, Question FROM QuestionToEmpoyees");
rptQuestionsEmp.DataBind();
}
}
private static DataTable GetData(string query)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string questionId = (e.Item.FindControl("hfquestionId") as HiddenField).Value;
Repeater rptAnswersEmp = e.Item.FindControl("rptAnswersEmp") as Repeater;
rptAnswersEmp.DataSource = GetData(string.Format("SELECT * FROM QuestionToEmpoyees WHERE QuestionId='{0}'", questionId));
rptAnswersEmp.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptQuestionsEmp.Items)
{
Repeater rptAns = item.FindControl("rptAnswersEmp") as Repeater;
foreach (RepeaterItem item1 in rptAns.Items)
{
bool option1 = (item1.FindControl("rbtnOpt1") as RadioButton).Checked;
bool option2 = (item1.FindControl("rbtnOpt2") as RadioButton).Checked;
bool option3 = (item1.FindControl("rbtnOpt3") as RadioButton).Checked;
bool option4 = (item1.FindControl("rbtnOpt4") as RadioButton).Checked;
if (option1 || option2 || option3 || option4)
{
// Do rest of Task.
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('Select one option from each question.');", true);
return;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
rptQuestionsEmp.DataSource = GetData("SELECT QuestionId, Question FROM QuestionToEmpoyees")
rptQuestionsEmp.DataBind()
End If
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = query
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
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("hfquestionId"), HiddenField)).Value
Dim rptAnswersEmp As Repeater = TryCast(e.Item.FindControl("rptAnswersEmp"), Repeater)
rptAnswersEmp.DataSource = GetData(String.Format("SELECT * FROM QuestionToEmpoyees WHERE QuestionId='{0}'", questionId))
rptAnswersEmp.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
For Each item As RepeaterItem In rptQuestionsEmp.Items
Dim rptAns As Repeater = TryCast(item.FindControl("rptAnswersEmp"), Repeater)
For Each item1 As RepeaterItem In rptAns.Items
Dim option1 As Boolean = (TryCast(item1.FindControl("rbtnOpt1"), RadioButton)).Checked
Dim option2 As Boolean = (TryCast(item1.FindControl("rbtnOpt2"), RadioButton)).Checked
Dim option3 As Boolean = (TryCast(item1.FindControl("rbtnOpt3"), RadioButton)).Checked
Dim option4 As Boolean = (TryCast(item1.FindControl("rbtnOpt4"), RadioButton)).Checked
If option1 OrElse option2 OrElse option3 OrElse option4 Then
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "message", "alert('Select one option from each question.');", True)
Return
End If
Next
Next
End Sub
Screenshot