2009-01-05 25 views
1

大家好後,我看不到我的動態生成單選按鈕列表或CheckBoxList的項目回傳

很顯然wehn我改變任何控件,然後回發的SaveViewState方法保存更改並回傳後應用它們agian,看到如下因素代碼通知,把服務器端代碼在scrispt在我的代碼liske這<%%>

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> 
     } 
    } 

現在我可以看到我的名單fileed但是當我選擇一個項目,按下一個按鈕SelectedItem屬性該清單仍爲空,是什麼原因?

+0

我不確定我是否理解你的問題?你有什麼問題? – brendan 2009-01-05 16:12:43

回答

0

感謝所有

我找到了原因,放置在.aspx文件中的代碼腳本被稱爲「代碼渲染塊」,它在呈現階段並保存視圖狀態階段,因此引起的更改完成後執行這種代碼只是呈現,但不保存,就這麼簡單。

1

嘗試在foreach之後執行rdlistAnswers.Databind()和cblistAnswers.Databind()。

+0

對不起,但它不工作,我猜RadioButtonList和CheckBoxList會自動綁定數據 – netseng 2009-01-06 08:10:32

0

試着將裏面的開關,如果(!的IsPostBack){...}

如果你不這樣做,它可能是MyCollection的沒有被正確填寫上回發這將解釋爲什麼你失去的項目。

+0

但我想在回發的情況下使用它,當我按下一個新的問題時,必須填寫甚至RadioButonList或CheckBoxList – netseng 2009-01-06 09:35:23

相關問題