在項(xiàng)目中經(jīng)常把一些經(jīng)常使用的代碼做成用戶控件以提高代碼的可重用性, 一個(gè)經(jīng)常遇到的就是在頁(yè)面中調(diào)用用戶控件中的服務(wù)器控件的事件,下面給出簡(jiǎn)單的代碼示列。
我們以一個(gè)用戶控件(a.ascx)中包含一個(gè) DropDownList 控件,然后在頁(yè)面(b.aspx)中調(diào)用 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)
...{
//選擇下拉列表時(shí)觸發(fā)
}
方法二:
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)
...{
//
}
聯(lián)系客服