C# asp.net网络控制事件顺序和视图状态
我有一个自定义类创建dropdownlist控件,如下所示:C# asp.net网络控制事件顺序和视图状态,c#,asp.net,C#,Asp.net,我有一个自定义类创建dropdownlist控件,如下所示: public class IHGridView : System.Web.UI.WebControls.WebControl { private string _dataSource = "not set yet"; public string DataSource { get { return _dataSource; } set { _dataSource = value;
public class IHGridView : System.Web.UI.WebControls.WebControl
{
private string _dataSource = "not set yet";
public string DataSource
{
get { return _dataSource; }
set { _dataSource = value; }
}
}
编辑:
或
所以,现在我得出一个结果,我必须在“OnInit”void中添加控件。
但是,这个“OnInit”是这个类编写的第一个空。
如果我想在“OnInit”void之前使用像“DataSource”这样的属性。。。
我该怎么做
编辑:
启动aspx页面中的按钮时会设置数据源。为什么要添加两次_dropDownList?一旦进入OnInit就足够了,如果您想将它添加到控件集合中,就应该在OnInit中添加它,这样就可以持久化和恢复viewstate 例如,要访问和绑定_dropDownList,请重写DataBind方法,此时所有属性都对您可用
protected override void DataBind(){
base.DataBind();
_dropDownList.DataSource = this.DataSource;
_dropDownList.DataBind();
}
这是伪代码,尚未经过测试或验证
编辑:
调用重写的数据绑定方法
protected void Button1_Click(object sender, EventArgs e)
{
IHGridViewTest2.DataSource = "fired";
IHGridViewTest2.DataBind();
}
当按钮被触发时。我编辑了问题。我的源代码中有“或”。也许你没有仔细阅读我的问题。无论如何,感谢您的回答。问题已编辑,但我的答案仍然适用,请维护添加到OnInit上的控件,然后如上所述重写DataBind方法,只需将按钮处理程序上的代码修改为:
protected void Button1_Click(object sender, EventArgs e)
{
IHGridViewTest2.DataSource = "fired";
}
protected override void DataBind(){
base.DataBind();
_dropDownList.DataSource = this.DataSource;
_dropDownList.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
IHGridViewTest2.DataSource = "fired";
IHGridViewTest2.DataBind();
}