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


Take a look at MSDN Link . It contains all the information and samples you need to create a CheckBoxList.



+1


source


netseng, take a look at custom controls, placeholders, and dynamically loading custom controls. I think with both of them you can make your page more dynamic and you can profit from being able to reuse custom controls!

0


source


I'm sure you don't need this:

i.Attributes.Add("runat", "server");

      

-1


source







All Articles