tags:

views:

367

answers:

4

Hi All I'm going to generate an asp:CheckBoxList using 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);

}

%>

Only the ListItem I added in the markup

(<'asp:ListItem Value="1">Me<'/asp:ListItem>)

appears, the other items I added dynamically doesn't appear, what I should do??

A: 

i'm sure you don't need this:

i.Attributes.Add("runat", "server");
karlis
+1  A: 

Have a look at this MSDN Link. It has all the details you require and samples for creating a CheckBoxList.

Ray Booysen
A: 

I Done It Just Like reversing Server-Side-Code to generate items with CheckBoxList tag, the reason is when executing in my first case adding items to the control comming after rendering it, and this is wrong, First add items to the control then write its tag to render.

I'm mixing my server-side-code with asp tags to dynamically generate my controls and show them, If any one has another way kindly provide.

Thanks All

netseng
A: 

netseng, have a look at user-controls, placeholders and dynamic loading of user-controls. i think with both of them you can make your page more dynamic, and, you will get a profit if you can reuse the user-controls!

karlis