I am trying to develop an online quiz application in .net. How do I use radiobuttonlist and datalist control to fufill this? I searched online that I could use radiobuttonlist bound with the datasource to populate questions and options inside the datalist
control using itemDataBound event,
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
//Check if the type is of Item
if (e.Item.ItemType == ListItemType.Item)
{
//Get the control form the current item.
RadioButtonList rbList = e.Item.FindControl("RadioButtonList1") as RadioButtonList;
rbList.DataSource = GetDataSource();
rbList.DataBind();
}
}
But how do I bound both questions and options in one radiobuttonlist control? or two? Can anyone give me some sample code or more information about this? TIA.
You have to get the QuestionId in the DataList ItemDataBound event and load the RadioButtonlist with the available options for the question
protectedvoid DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item) { RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1"); //Get questionID here int QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionID")); //pass Question ID to your DB and get all available options for the question //Bind the RadiobUttonList here }
Thank you so much for the answer. I throw some test data into the question table and choice table. But the question 2 options are missing. Other two questions and options are displayed fine. I checked the database and code but I couldn't figure out where
is the problem. Please see my code below:
<asp:DataList ID="DataList1" runat="server" DataKeyField="questionid"
onitemdatabound="DataList1_ItemDataBound" DataSourceID="SqlDataSource1">
<ItemTemplate> <br />
<asp:Label ID="questiontextLabel" runat="server"
Text='<%# Eval("questiontext") %>' />
<br /> <br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList> </ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connectionstr %>"
SelectCommand="SELECT questionid, questiontext FROM dbo.question">
</asp:SqlDataSource>
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{ if (e.Item.ItemType == ListItemType.Item)
{ RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
//Get questionID here
int QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionID"));
//pass Question ID to your DB and get all available options for the question
string strConn = WebConfigurationManager.ConnectionStrings["connectionstr"].ConnectionString;
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string sql=string.Empty;
sql = "SELECT choice.optionid,choice.choicetext FROM [question] inner join choice on question.questionid= choice.questionid and question.module=choice.module where question.questionid="+ QuestionID;
//Bind the RadiobUttonList here
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds); DataTable dt = ds.Tables[0];
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = dt.Columns[1].ToString();
RadioButtonList1.DataValueField = dt.Columns[0].ToString();
RadioButtonList1.DataBind(); } }
One more thing, can I retrieve the questionid also? I want to insert user's selection including questionid, optionid into the database. I can certainly run a query to get the questionid based on the value (optionid) that user selected. I am just wondering
if there is an easy way to get the questionid.
jj819
Member
168 Points
145 Posts
radiobuttonlist inside datalist
Feb 15, 2011 05:16 PM|LINK
Hi All,
I am trying to develop an online quiz application in .net. How do I use radiobuttonlist and datalist control to fufill this? I searched online that I could use radiobuttonlist bound with the datasource to populate questions and options inside the datalist control using itemDataBound event,
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { //Check if the type is of Item if (e.Item.ItemType == ListItemType.Item) { //Get the control form the current item. RadioButtonList rbList = e.Item.FindControl("RadioButtonList1") as RadioButtonList; rbList.DataSource = GetDataSource(); rbList.DataBind(); } }But how do I bound both questions and options in one radiobuttonlist control? or two? Can anyone give me some sample code or more information about this? TIA.
.NET 3.5
sansan
All-Star
53942 Points
8147 Posts
Re: radiobuttonlist inside datalist
Feb 15, 2011 05:25 PM|LINK
<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; margin: 8px;">
</div>It depends on how your layout is. This is one possibility from your above code
You have to get the QuestionId in the DataList ItemDataBound event and load the RadioButtonlist with the available options for the question
jj819
Member
168 Points
145 Posts
Re: radiobuttonlist inside datalist
Feb 15, 2011 08:17 PM|LINK
Thank you so much for the answer. I throw some test data into the question table and choice table. But the question 2 options are missing. Other two questions and options are displayed fine. I checked the database and code but I couldn't figure out where is the problem. Please see my code below:
<asp:DataList ID="DataList1" runat="server" DataKeyField="questionid" onitemdatabound="DataList1_ItemDataBound" DataSourceID="SqlDataSource1"> <ItemTemplate> <br /> <asp:Label ID="questiontextLabel" runat="server" Text='<%# Eval("questiontext") %>' /> <br /> <br /> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> </asp:RadioButtonList> </ItemTemplate> </asp:DataList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionstr %>" SelectCommand="SELECT questionid, questiontext FROM dbo.question"> </asp:SqlDataSource> protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item) { RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1"); //Get questionID here int QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionID")); //pass Question ID to your DB and get all available options for the question string strConn = WebConfigurationManager.ConnectionStrings["connectionstr"].ConnectionString; SqlConnection conn = new SqlConnection(strConn); conn.Open();string sql=string.Empty; sql = "SELECT choice.optionid,choice.choicetext FROM [question] inner join choice on question.questionid= choice.questionid and question.module=choice.module where question.questionid="+ QuestionID; //Bind the RadiobUttonList here SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; RadioButtonList1.DataSource = dt; RadioButtonList1.DataTextField = dt.Columns[1].ToString(); RadioButtonList1.DataValueField = dt.Columns[0].ToString(); RadioButtonList1.DataBind(); } }sansan
All-Star
53942 Points
8147 Posts
Re: radiobuttonlist inside datalist
Feb 15, 2011 08:23 PM|LINK
Sorry, I should be checking for itemType as Alternating item along with Item
jj819
Member
168 Points
145 Posts
Re: radiobuttonlist inside datalist
Feb 15, 2011 08:31 PM|LINK
Thank you!!! It works perfectly now
rivdiv
All-Star
16323 Points
2595 Posts
Re: retrieve radiobuttonlist value in datalist
Feb 16, 2011 07:20 PM|LINK
You could loop through each DataListItem and get the value like:
foreach (DataListItem item in DataList1.Items) { RadioButtonList rbl = (RadioButtonList)item.FindControl("RadioButtonList1"); if (rbl != null) { string value = rbl.SelectedValue; } }jj819
Member
168 Points
145 Posts
Re: retrieve radiobuttonlist value in datalist
Feb 16, 2011 07:50 PM|LINK
Thank you! Works great.
jj819
Member
168 Points
145 Posts
Re: retrieve radiobuttonlist value in datalist
Feb 16, 2011 08:21 PM|LINK
One more thing, can I retrieve the questionid also? I want to insert user's selection including questionid, optionid into the database. I can certainly run a query to get the questionid based on the value (optionid) that user selected. I am just wondering if there is an easy way to get the questionid.
rivdiv
All-Star
16323 Points
2595 Posts
Re: retrieve radiobuttonlist value in datalist
Feb 16, 2011 08:25 PM|LINK
Provided that you have the DataKeyField set, you could get it with:
string questionID = DataList1.DataKeys[item.ItemIndex].ToString();
(looks like this was merged with your original thread)
jj819
Member
168 Points
145 Posts
Re: retrieve radiobuttonlist value in datalist
Feb 17, 2011 12:56 PM|LINK
Great! Works like a charm. Thank you.