您的位置:首页 > 其它

关于页面中获取用户控件中的控件事件的方法

2009-11-27 10:20 656 查看
在项目中经常把一些经常使用的代码做成用户控件以提高代码的可重用性, 一个经常遇到的就是在页面中调用用户控件中的服务器控件的事件,下面给出简单的代码示列。

我们以一个用户控件(a.ascx)中包含一个 DropDownList 控件,然后在页面(b.aspx)中调用 DropDownList 的 SelectedIndexChanged 为列

方法一:

a.ascx .cs

public DropDownList innerDropDownList
...{
get ...{ return DropDownList1; }
}

b.aspx

protected void Page_Load(object sender, EventArgs e)
...{

this.a1.innerDropDownList.SelectedIndexChanged += new EventHandler(UserControl_Clicked);

}
private void UserControl_Clicked(object sender, System.EventArgs e)
...{
//选择下拉列表时触发

}

方法二:

a.ascx.cs

public EventHandler eventSelect;
protected void ddlcolor_SelectedIndexChanged(object sender, EventArgs e)
...{
if (this.eventSelect != null)
...{
this.eventSelect(this, e);
}
}
b.aspx.cs
protected void Page_Load(object sender, EventArgs e)
...{

this.a1.eventSelect += new EventHandler(UserControl_Clicked);
}
private void UserControl_Clicked(object sender, System.EventArgs e)
...{
//
}

上一页 1 下一页 1/1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐