[英]asp.net WebControl event order and ViewState
我有一個自定義類,創建一個dropdownlist控件,如下所示:
public class IHGridView : System.Web.UI.WebControls.WebControl
{
private string _dataSource = "not set yet";
public string DataSource
{
get { return _dataSource; }
set { _dataSource = value; }
}
}
編輯:
protected override void OnInit(EventArgs e)
{
// VIewState is alive. When I select an option and submit, after postback it's selected value is the one I selected.
this.Controls.Add(_dropDownList);
}
要么
protected override void CreateChildControls()
{
// VIewState is dead. When I select an option and submit, after postback it's selected value is the default one.
this.Controls.Add(_dropDownList);
}
因此,現在我想出了必須在“ OnInit”無效中添加控件的結果。 但是,此“ OnInit”是此類寫入的第一個空白。 如果我想在之前使用“ DataSource”之類的屬性,則“ OnInit”無效...我該怎么做?
編輯:
protected void Button1_Click(object sender, EventArgs e)
{
IHGridViewTest2.DataSource = "fired";
}
觸發aspx頁面中的按鈕時,將設置DataSource。
您為什么要兩次添加_dropDownList? 在OnInit中一次就足夠了,如果需要,可以在控件集合中添加OnInit-這樣就可以保留和恢復viewstate。
例如,要訪問和綁定_dropDownList,請重寫DataBind方法-此時所有屬性將對您可用。
protected override void DataBind(){
base.DataBind();
_dropDownList.DataSource = this.DataSource;
_dropDownList.DataBind();
}
這是偽代碼,尚未經過測試或驗證
編輯:
調用重寫的DataBind方法
protected void Button1_Click(object sender, EventArgs e)
{
IHGridViewTest2.DataSource = "fired";
IHGridViewTest2.DataBind();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.