在项目中经常把一些经常使用的代码做成用户控件以提高代码的可重用性, 一个经常遇到的就是在页面中调用用户控件中的服务器控件的事件,下面给出简单的代码示列。
我们以一个用户控件(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转载于:https://www.cnblogs.com/lizh0103/archive/2009/03/16/1413614.html
相关资源:JAVA上百实例源码以及开源项目