Hi
In below code Options are not appearing with Radio Buttons.
Only 1 Radio Button can be selected per row.
namespace MyApp
{
public partial class Questionaire : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rptQuestions.DataSource = GetData("SELECT QuestionId, Question, Option1, Option2, Option3, Option4 FROM QuestionToEmpoyees");
rptQuestions.DataBind();
}
}
private static DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["Cnn0"].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 itemDataBoundQuestion(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 rptAnswer = e.Item.FindControl("rptAnswers") as Repeater;
rptAnswer.DataSource = GetData(string.Format("SELECT EmployeeId,[Employee Name],QuestionId,Question,Option1,Option2,Option3,Option4 FROM QuestionToEmpoyees,EmpoyeesAnswers WHERE QuestionId='{0}'", questionId));
rptAnswer.DataBind();
}
}
}
}
<asp:Repeater ID="rptQuestions" runat="server" OnItemDataBound="itemDataBoundQuestion">
<HeaderTemplate>
<table>
<tr></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>Question
<asp:Label ID="lblQuestionId" runat="server" Text='<%# Eval("QuestionId") %>' />:</td>
<td colspan="4"><asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>' /></td>
</tr>
<tr>
<asp:HiddenField ID="hfQuestionID" runat="server" Value='<%#Eval("QuestionId")%>' />
<asp:Repeater ID="rptAnswers" runat="server">
<ItemTemplate>
<tr>
<%-- <td>
<asp:Label ID="lblEmployeeId" runat="server" Text='<%# Eval("EmployeeId") %>' />
</td>--%>
<td><asp:Label ID="lblEmployeeName" runat="server" Text='<%# Eval("[Employee Name]") %>' />:
</td>
<td><asp:Label ID="lblOpt1" runat="server" Text='<%# Eval("Option1") %>' /></td>
<td><asp:Label ID="lblOpt2" runat="server" Text='<%# Eval("Option2") %>' /></td>
<td><asp:Label ID="lblOpt3" runat="server" Text='<%# Eval("Option3") %>' /></td>
<td><asp:Label ID="lblOpt4" runat="server" Text='<%# Eval("Option4") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
CREATE TABLE [QuestionToEmpoyees](
[QuestionId] INT IDENTITY (1,1),
[Question] VARCHAR(300),
[Option1] VARCHAR(100),
[Option2] VARCHAR(100),
[Option3] VARCHAR(100),
[Option4] VARCHAR(100)
)
INSERT INTO [QuestionToEmpoyees] VALUES('Capital City of Maharshtra', 'Mumbai', 'Delhi', 'Kolkata', 'Haryana')
INSERT INTO [QuestionToEmpoyees] VALUES('The power to decide an election petition is vested in the', 'Parliament', 'High Court', 'Supreme Court', 'Others')
CREATE TABLE [EmpoyeesAnswers](
[EmployeeId] INT,
[Employee Name] VARCHAR(100)
)
INSERT INTO [EmpoyeesAnswers] VALUES(1, 'Employee-A')
INSERT INTO [EmpoyeesAnswers] VALUES(2, 'Employee-B')
INSERT INTO [EmpoyeesAnswers] VALUES(3, 'Employee-C')
INSERT INTO [EmpoyeesAnswers] VALUES(4, 'Employee-D')
Thanks