2011-02-25 42 views
0

下面是我的動態下拉列表的代碼。它確實生成了HTML。但是,該事件並未被解僱。而且,當我將事件名稱更改爲「onchange」時,它給我一個編譯錯誤,說它找不到腳本。通過它在我的代碼後面。動態創建DropDownList不觸發事件。是的,Autopostback設置爲TRUE

另外,我在OnInit頁面事件中添加了這個。

pValueCmbBox.Attributes.Add("runat", "server"); 
pValueCmbBox.SelectedIndexChanged += new EventHandler(ddlParent_SelectedIndexChanged); 
pValueCmbBox.Attributes.Add("OnSelectedIndexChanged", "ddlParent_SelectedIndexChanged"); 
pValueCmbBox.Attributes.Add("AutoPostBack", "True"); 
  1. 爲什麼不OnSelectedIndexChanged被解僱?
  2. 「onchange」僅用於調用JavaScript?
  3. 我應該把它作爲一個ASCX來實現嗎?

回答

0

回答我的問題...

我最終作出一個ASCX了它,現在它工作正常!

0

1)它不起作用,因爲您沒有以您應該的方式添加屬性「AutoPostBack」。

pValueCmbBox.Attributes.Add("runat", "server"); //doesn't make sense...it's just for decoration...because you can't use in page behind code 
pValueCmbBox.SelectedIndexChanged += new EventHandler(ddlParent_SelectedIndexChanged); //this line it's okay 
pValueCmbBox.Attributes.Add("OnSelectedIndexChanged", "ddlParent_SelectedIndexChanged"); //this it's not necessary at all...because you already specified through pValueCmbBox.SelectedIndexChanged 
pValueCmbBox.Attributes.Add("AutoPostBack", "True"); //this is the problem 

正如你可以看到here的SelectedIndexChanged「時發生從帖子的列表控件更改服務器的選擇。」 。所以你有一個關於AutoPostBack = true的好主意;你應該寫:

pValueCmbBox.AutoPostBack = true; 

現在的runat="server"問題,您可以設置背後的功能您的頁面如下:

protected void ddlParent_SelectedIndexChanged(object sender, EventArgs e) 
{ 
DropDownList c = (DropDownList)sender; //this is your pValueCmbBox that you set it in OnInit 
//more code here 
} 

2)平變化是Javascript,但對於C#/ VB你可以使用OnTextChanged
3)你可以這樣做,因爲你已經嘗試過......或者我告訴過你的方式。 :)

相關問題