tags:

views:

358

answers:

3

Hi All,

It's clear wehn I change any control and then posts back the SaveViewState method saves the changes and apply them agian after postback, see the folowing code notice that server-side-code put in a scrispt in my code liske this <% %>

switch (myQuestion.QuestionType)
{
        case 0:
        {
             rdlistAnswers.Items.Clear();
             foreach (sting item in myCollection)
             {
                  ListItem i = new ListItem();
                  i.Value = item;
                  i.Text = item;
                  rdlistAnswers.Items.Add(i);
             }
     **//the folowing line of code is not a comment, it's a tag for asp control
     //but I commeted it due to editing requirements**

     //<asp:RadioButtonList ID="rdlistAnswers" runat="server"</asp:RadioButtonList>


            break;
        }
        case 1:
        {
            cblistAnswers.Items.Clear();
            foreach (sting item in myCollection)
            {
                 ListItem i = new ListItem();
                 i.Value = item;
                 i.Text = item;
                 cblistAnswers.Items.Add(i);
            }

        <asp:CheckBoxList ID="cblistAnswers" runat="server" </asp:CheckBoxList>
        }
    }

Now I Can see my list fileed but when I select an item and push next button the SelectedItem property of the list remains null, what's the reason??

+1  A: 

try to do rdlistAnswers.Databind() and cblistAnswers.Databind() after the foreach.

Sergio
Sorry but it doesn't work, I guess RadioButtonList and CheckBoxList binds data automatically
netseng
A: 

Try placing that switch inside an if(!isPostback){...}

If you are not doing this its possible that myCollection is not being properly filled on postback and that would explain why you lose the items.

Sergio
But I wnat to use it in case of postback, when I press next a new question choices have to be filled in even RadioButonList or CheckBoxList
netseng
A: 

Thanks All

I found the reason, the code script placed in .aspx file is called "code render block", its executed during the render phase and after save view state phase so the changes done by this kind of code just renderd but not saved, as simple as that.

netseng