Dynamically creating a RadioButtonList
I am going to generate asp:CheckBoxList
with this code
<%
System.Collections.Generic.List<QuizzEngine.Common.Question> qList = (System.Collections.Generic.List<QuizzEngine.Common.Question>)Session[QuizzEngine.Common.SessionKeys.QuesionsList];
int navigator = (int)Session[QuizzEngine.Common.SessionKeys.Navigator];
if (True)
{
%>
<'asp:CheckBoxList ID="cblistAnswers" runat="server"
<'Width="139px"style="text-align: left" AutoPostBack="False">
<'asp:ListItem Value="1">Me<'/asp:ListItem>
<'/asp:CheckBoxList>
<%
cblistAnswers.Items.Clear();
foreach (QuizzEngine.Common.Answer answer in qList[navigator].QuestionAllAnswers)
{
ListItem i = new ListItem();
i.Attributes.Add("runat", "server");
i.Value = answer.AnswerId.ToString();
i.Text = answer.AnswerText;
cblistAnswers.Items.Add(i);
}
%>
In the markup, I only added ListItem
:
<'asp:ListItem Value="1">Me<'/asp:ListItem>
other items that I added dynamically are not showing, what should I do?
-1
source to share
4 answers
I did it The same as reversing the Server-Side-Code to generate items with a CheckBoxList tag , the reason is that the first time I run it I am adding items to the control, then it pops up and it is wrong. First add elements to the control and then write its tag to render.
I am mixing my server-side code with asp tags to dynamically generate my controls and display them. If anyone has any other way, please kindly provide.
Thanks everyone
0
source to share