2012-05-30 205 views
1

我建立一個動態的單選按鈕列表列出所有的記錄,將特定搜索項目,並允許用戶選擇相關選項。然而,我遇到的問題是SelectedInhdexChanged事件永遠不會觸發。ASP.Net RadioButtonList的SelectedIndexChanged事件不觸發

我已經試過初始化在RadioButtonList和在Page_Load和page_init方法分配的事件處理程序。我也試着拖動單選按鈕列表到頁面上,雙擊它來創建事件處理程序way-但仍沒有運氣。

任何想法?我已經粘貼下面給你看看我的代碼:

這裏是我的Page_Load和事件處理方法:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      measureDropdown = loadDropdown("GetMeasuringTypes", measureDropdown); 
      categoryDropdown = loadDropdown("GetCategories", categoryDropdown); 
     } 

     rBtn = new RadioButtonList(); 
     rBtn.CausesValidation = true; 
     rBtn.SelectedIndexChanged += new EventHandler(rBtn_SelectedIndexChanged); 
    } 

    void rBtn_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     moreThanOneLbl.Text = "Woohoo!"; 
    } 

下面是我給你的列表項的單選按鈕列表(從數據表):

foreach (DataRow row in table.Rows) 
      { 
       ListItem li = new ListItem(); 
       li.Value = row[0].ToString(); 
       li.Text = row[1].ToString() + ": " + row[2].ToString(); 
       //rBtn.Items.Add(li); 
       RadioButtonList1.Items.Add(li); 

      } 
+0

您是否在標記視圖的控件上命名了事件,例如: 'OnSelectedChanged =「the_event」'在控制所有又回到了我 - 回首我發佈的代碼 – dtsg

回答

11

嘗試設置AutoPostBack屬性爲true。

rBtn.AutoPostBack = true 
+0

謝謝,這是一個有點混亂,因爲它是引用兩個不同的單選按鈕列表(在標記視圖和其他定義的一個在代碼隱藏中定義)。兩人都做了完全一樣的事情,他們都沒有工作。然而,在標記視圖中將Blachsma的建議添加到單選按鈕列表中仍然有效!歡呼! – donpisci

相關問題