I can't see my dynamically created RadioButtonList or CheckBoxList objects after postback

Hello to all,

It is clear that I change any control and then go back the SaveViewState method saves the changes and applies them agian after postback, see the following post about server side code being scripted 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 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 view the list, but when I select an item and click the next button, the SelectedItem property of the list remains null , which is the reason

+1


source to share


3 answers


Thanks everyone



I found the reason, the script code placed in the .aspx file is called the "code rendering block" that is executed during the render phase and after the save state phase, so changes made with this type of code are just renderd but not saved , the same just.

0


source


try doing rdlistAnswers.Databind () and cblistAnswers.Databind () after foreach.



+1


source


Try to put this switch inside if (! IsPostback) {...}

If you don't, it is possible that myCollection will not be properly populated on postback, which will explain why you are losing items.

0


source







All Articles