一、簡(jiǎn)單實(shí)例
先來(lái)個(gè)簡(jiǎn)單點(diǎn)的,首先新建一個(gè) “web用戶控件”WebUserControl1.ascx,
在頁(yè)面上添加一個(gè)按鈕,并響應(yīng)Click事件:
- <asp:Button ID="Button1" runat="server" Text="在頁(yè)面級(jí)的TextBox輸入一個(gè)隨機(jī)數(shù)" onclick="Button1_Click" />
然后編寫(xiě)后臺(tái)代碼:
-
- public event EventHandler Click;
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- Click(this, EventArgs.Empty);
- }
好了,用戶控件我們就算完成了,現(xiàn)在新建一個(gè)WebForm1.aspx頁(yè)面,在頁(yè)面上添加一個(gè)Textbox,并把用戶控件添加到該頁(yè)面:
- <%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <br />
- <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
- </div>
- </form>
- </body>
- </html>
然后我們?cè)诤笈_(tái)對(duì)用戶控件的事件添加一個(gè)函數(shù)響應(yīng),代碼如下:
- protected void Page_Load(object sender, EventArgs e)
- {
- WebUserControl11.Click += new EventHandler(WebUserControl11_Click);
- }
-
- void WebUserControl11_Click(object sender, EventArgs e)
- {
- TextBox1.Text = new Random().Next(1000, 9999).ToString();
- }
到這,我們的示例就完成了,我們?cè)赼spx可以捕捉到用戶控件的事件,做我們想做的事情了。
二、帶有事件數(shù)據(jù)的示例
當(dāng)然,有時(shí)候我們不僅想要在aspx頁(yè)面捕捉到事件,還希望能獲取到一些參數(shù),這時(shí)候EventArgs就需要出場(chǎng)了
首先,新建一個(gè)web用戶控件WebUserControl2.ascx
在頁(yè)面上添加兩個(gè)按鈕,分別響應(yīng)Click事件,如下:
- <asp:Button ID="Button1" runat="server" Text="修改頁(yè)面標(biāo)題為編輯"
- onclick="Button1_Click" />
- <asp:Button ID="Button2" runat="server" Text="修改頁(yè)面標(biāo)題為新增"
- onclick="Button2_Click" />
C#代碼,首先添加一個(gè)類ChangePageTitleEventArgs,用來(lái)傳遞事件數(shù)據(jù),代碼如下:
- public class ChangePageTitleEventArgs : EventArgs
- {
- public ChangePageTitleEventArgs(string title)
- {
- this.Title = title;
- }
-
- public string Title
- {
- get;
- private set;
- }
-
- }
然后我們來(lái)編寫(xiě)用戶控件的后臺(tái)代碼:
- public event EventHandler<ChangePageTitleEventArgs> ChangePageTitle;
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- ChangePageTitle(this,new ChangePageTitleEventArgs("編輯"));
- }
-
- protected void Button2_Click(object sender, EventArgs e)
- {
- ChangePageTitle(this, new ChangePageTitleEventArgs("新增"));
- }
用戶控件的代碼就OK了,然后我們新建一個(gè)WebForm2.aspx頁(yè)面,將用戶控件添加到該頁(yè)面
- <%@ Register src="WebUserControl2.ascx" tagname="WebUserControl2" tagprefix="uc1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <uc1:WebUserControl2 ID="WebUserControl21" runat="server" />
- </form>
- </body>
- </html>
編寫(xiě)后臺(tái)代碼,對(duì)用戶控件的時(shí)間添加處理函數(shù):
- protected void Page_Load(object sender, EventArgs e)
- {
- WebUserControl21.ChangePageTitle += new EventHandler<ChangePageTitleEventArgs>(WebUserControl21_ChangePageTitle);
- }
-
- void WebUserControl21_ChangePageTitle(object sender, ChangePageTitleEventArgs e)
- {
- this.Page.Title = e.Title;
- }
好了,很簡(jiǎn)單的我們就完成了代碼,運(yùn)行測(cè)試一下,效果如下: