2012-04-10 12 views
1

我有一個頁面來執行heirarchical搜索,它從一個下拉列表開始,並基於在下拉列表中選擇的值來查詢數據庫並顯示孩子在另一個下拉列表中,並且只要它碰到葉片就會繼續......因此,我首先動態添加了下拉列表,並且它具有SelectedIndexChanged上的事件處理程序,當我更改所選值時,會觸發回傳,但是不調用事件處理程序方法 ..不知道我在這裏做錯了什麼?或者它是一個錯誤?asp.net - 動態創建的下拉列表不會在回發中調用事件處理程序方法

使用一個會話變量來跟蹤創建的控件

private List<DynamicControlProperties> PersistedControls 
    { 
     get 
     { 
      if (_persistedControls == null) 
      { 
       if (Session[PersistedControlsKey] == null) 
       { 
        Session[PersistedControlsKey] = new List<DynamicControlProperties>(); 
       } 

       _persistedControls = Session[PersistedControlsKey] as List<DynamicControlProperties>; 
      } 

      return _persistedControls; 
     } 
    } 

和頁初始化,重新創建動態生成的控制

protected override void LoadViewState(object savedState) 
    { 
     base.LoadViewState(savedState); 

     // regenerate the persisted controls 
     foreach (var prop in PersistedControls) 
     { 
      CreateControl(prop); 
     } 

    } 

在頁面加載,創建了第一個下拉

protected void Page_Load(object sender, EventArgs e) 
    { 


     if (!Page.IsPostBack) 
     { 
      // create the control 
      CreateControl(....) 

      // bind the data to the dropdown 
     } 
    } 

在創建控制方法中,只需創建一個標籤和一個下拉環繞它裏面並添加它來佔位

private DropDownList CreateControl(DynamicControlProperties dynamiccntrlprop) 
    { 
     // create a new HTML row 
     HtmlGenericControlWithParentID tr = new HtmlGenericControlWithParentID("tr"); 
     HtmlGenericControlWithParentID td1 = new HtmlGenericControlWithParentID("td"); 
     HtmlGenericControlWithParentID td2 = new HtmlGenericControlWithParentID("td"); 

     // make sure we set the id and parentid 
     tr.ID = string.Format("tr{0}", dynamiccntrlprop.ID);    
     tr.ParentID = dynamiccntrlprop.ParentID; 
     tr.EnableViewState = true; 

     // create a new label for dropdown 
     Label lbl = new Label() { ID = string.Format("lbl{0}", dynamiccntrlprop.DisplayName), Text = dynamiccntrlprop.DisplayName }; 

     // create a new dropdown list 
     DropDownList ddl = new DropDownList() 
     { 
      ID = string.Format("ddl{0}", dynamiccntrlprop.DisplayName), 

      // set the postback 
      AutoPostBack = true, 

      EnableViewState = true 
     }; 

     // subscribe for the select index changed event 
     ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); 

     // add the controls to table row 
     td1.Controls.Add(lbl); 
     td2.Controls.Add(ddl); 

     tr.Controls.Add(td1); 
     tr.Controls.Add(td2); 

     // add the control to place holder  
     this.filtersPlaceHolder.Controls.Add(tr); 

     return ddl; 

    } 

這裏是指數變化的處理程序,

protected void ddl_SelectedIndexChanged(object sender, EventArgs e) 
{ 
} 

啓用視圖狀態,自動回等等等等......重新用相同的ID的控件在回發..在谷歌嘗試所有的答案..但沒有運氣..它確實觸發回發時,我改變了索引,但沒有調用事件處理方法..

任何想法,請?

非常感謝, ķ

回答

1

你必須確保createControl方法被稱爲對每一個網頁提交。這需要發生以確保動態控件的事件處理程序在回發後被掛鉤。

protected void Page_Load(object sender, EventArgs e) 
{ 
    // you shouldn't wrap the call to CreateControl in this 'if' statement 
    //if (!Page.IsPostBack) 
    //{ 
      // create the control 
      CreateControl(....) 

      // bind the data to the dropdown 
    //} 
} 

一旦你這樣做,選定的索引更改事件將觸發。

+0

@karthik:你嘗試過這一點,它的工作原理? – Marcel 2013-08-07 11:31:08

0

也許這是因爲下載列表的新值不被加載。 保護覆蓋無效LoadViewState(對象savedState) {

// regenerate the persisted controls 
    foreach (var prop in PersistedControls) 
    { 
     CreateControl(prop); 
    } 

    base.LoadViewState(savedState); 
} 
相關問題