用户控件事件使用delegate



用户控件事件使用delegate

1.在我们写一个用户控件时需要考虑到重用得问题,如果控件中包含按钮事件,


我们不可能将点击事件写到控件里,而是我们想吧事件处理得过程写在


调用控件的页面中,这是该怎么处理呢?


我的做法时使用delegate来实现这个功能!


具体做法如下:

下面是控件的html部分

 1用户控件事件使用delegate_用户控件用户控件事件使用delegate_点击事件_02<%用户控件事件使用delegate_html_03@ Control Language="C#" AutoEventWireup="true" CodeFile="ctlForm.ascx.cs" Inherits="ctlForm" %>

 2用户控件事件使用delegate_html_04<table>

 3用户控件事件使用delegate_html_04    <tr>

 4用户控件事件使用delegate_html_04        <td style="width: 100px">

 5用户控件事件使用delegate_html_04            name</td>

 6用户控件事件使用delegate_html_04        <td style="width: 100px">

 7用户控件事件使用delegate_html_04            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>

 8用户控件事件使用delegate_html_04    </tr>

 9用户控件事件使用delegate_html_04    <tr>

10用户控件事件使用delegate_html_04        <td style="width: 100px">

11用户控件事件使用delegate_html_04            sex</td>

12用户控件事件使用delegate_html_04        <td style="width: 100px">

13用户控件事件使用delegate_html_04            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>

14用户控件事件使用delegate_html_04    </tr>

15用户控件事件使用delegate_html_04    <tr>

16用户控件事件使用delegate_html_04        <td style="width: 100px">

17用户控件事件使用delegate_html_04        </td>

18用户控件事件使用delegate_html_04        <td style="width: 100px">

19用户控件事件使用delegate_html_04            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="测试" /></td>

20用户控件事件使用delegate_html_04    </tr>

21用户控件事件使用delegate_html_04</table>

22用户控件事件使用delegate_html_04


控件的cs部分

 1用户控件事件使用delegate_html_04using System;

 2用户控件事件使用delegate_html_04using System.Data;

 3用户控件事件使用delegate_html_04using System.Configuration;

 4用户控件事件使用delegate_html_04using System.Collections;

 5用户控件事件使用delegate_html_04using System.Web;

 6用户控件事件使用delegate_html_04using System.Web.Security;

 7用户控件事件使用delegate_html_04using System.Web.UI;

 8用户控件事件使用delegate_html_04using System.Web.UI.WebControls;

 9用户控件事件使用delegate_html_04using System.Web.UI.WebControls.WebParts;

10用户控件事件使用delegate_html_04using System.Web.UI.HtmlControls;

11用户控件事件使用delegate_html_04

12用户控件事件使用delegate_html_04public partial class ctlForm : System.Web.UI.UserControl

13用户控件事件使用delegate_用户控件用户控件事件使用delegate_点击事件_02用户控件事件使用delegate_html_03{

14用户控件事件使用delegate_html_40    protected void Page_Load(object sender, EventArgs e)

15用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

16用户控件事件使用delegate_html_40

17用户控件事件使用delegate_html_45    }

18用户控件事件使用delegate_html_40

19用户控件事件使用delegate_html_40    public delegate void ClickHander();

20用户控件事件使用delegate_html_40

21用户控件事件使用delegate_html_40    public ClickHander MyClickHandler = null;

22用户控件事件使用delegate_html_40

23用户控件事件使用delegate_html_40    public void Button1_Click(object sender, EventArgs e)

24用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

25用户控件事件使用delegate_html_40        if (MyClickHandler != null)

26用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42        用户控件事件使用delegate_html_03{

27用户控件事件使用delegate_html_40            MyClickHandler();

28用户控件事件使用delegate_html_45        }

29用户控件事件使用delegate_html_45    }

30用户控件事件使用delegate_html_62}

31用户控件事件使用delegate_html_04



我们调用这个控件的页面写法如下:

 1用户控件事件使用delegate_html_04<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testForm.aspx.cs" Inherits="testForm" %>

 2用户控件事件使用delegate_html_04

 3用户控件事件使用delegate_html_04<%@ Register Src="ctlForm.ascx" TagName="ctlForm" TagPrefix="uc1" %>

 4用户控件事件使用delegate_html_04

 5用户控件事件使用delegate_html_04<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 6用户控件事件使用delegate_html_04

 7用户控件事件使用delegate_html_04<html xmlns="http://www.w3.org/1999/xhtml" >

 8用户控件事件使用delegate_html_04<head runat="server">

 9用户控件事件使用delegate_html_04    <title>无标题页</title>

10用户控件事件使用delegate_html_04</head>

11用户控件事件使用delegate_html_04<body>

12用户控件事件使用delegate_html_04    <form id="form1" runat="server">

13用户控件事件使用delegate_html_04    <div>

14用户控件事件使用delegate_html_04        <uc1:ctlForm ID="CtlForm1" runat="server" />

15用户控件事件使用delegate_html_04    

16用户控件事件使用delegate_html_04    </div>

17用户控件事件使用delegate_html_04    </form>

18用户控件事件使用delegate_html_04</body>

19用户控件事件使用delegate_html_04</html>

20用户控件事件使用delegate_html_04

调用控件的cs代码如下


 1用户控件事件使用delegate_html_04using System;

 2用户控件事件使用delegate_html_04using System.Data;

 3用户控件事件使用delegate_html_04using System.Configuration;

 4用户控件事件使用delegate_html_04using System.Collections;

 5用户控件事件使用delegate_html_04using System.Web;

 6用户控件事件使用delegate_html_04using System.Web.Security;

 7用户控件事件使用delegate_html_04using System.Web.UI;

 8用户控件事件使用delegate_html_04using System.Web.UI.WebControls;

 9用户控件事件使用delegate_html_04using System.Web.UI.WebControls.WebParts;

10用户控件事件使用delegate_html_04using System.Web.UI.HtmlControls;

11用户控件事件使用delegate_html_04

12用户控件事件使用delegate_html_04public partial class testForm : System.Web.UI.Page

13用户控件事件使用delegate_用户控件用户控件事件使用delegate_点击事件_02用户控件事件使用delegate_html_03{

14用户控件事件使用delegate_html_40    protected void Page_Load(object sender, EventArgs e)

15用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

16用户控件事件使用delegate_html_40        CtlForm1.MyClickHandler = new ctlForm.ClickHander(this.Test);

17用户控件事件使用delegate_html_45    }

18用户控件事件使用delegate_html_40

19用户控件事件使用delegate_html_40    public void Test()

20用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

21用户控件事件使用delegate_html_40        Response.Write("ok");

22用户控件事件使用delegate_html_45    }

23用户控件事件使用delegate_html_40

24用户控件事件使用delegate_html_40    

25用户控件事件使用delegate_html_62}



作者:水木    


 


 


用户控件事件使用delegate

1.在我们写一个用户控件时需要考虑到重用得问题,如果控件中包含按钮事件,


我们不可能将点击事件写到控件里,而是我们想吧事件处理得过程写在


调用控件的页面中,这是该怎么处理呢?


我的做法时使用delegate来实现这个功能!


具体做法如下:

下面是控件的html部分

 1用户控件事件使用delegate_用户控件用户控件事件使用delegate_点击事件_02<%用户控件事件使用delegate_html_03@ Control Language="C#" AutoEventWireup="true" CodeFile="ctlForm.ascx.cs" Inherits="ctlForm" %>

 2用户控件事件使用delegate_html_04<table>

 3用户控件事件使用delegate_html_04    <tr>

 4用户控件事件使用delegate_html_04        <td style="width: 100px">

 5用户控件事件使用delegate_html_04            name</td>

 6用户控件事件使用delegate_html_04        <td style="width: 100px">

 7用户控件事件使用delegate_html_04            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>

 8用户控件事件使用delegate_html_04    </tr>

 9用户控件事件使用delegate_html_04    <tr>

10用户控件事件使用delegate_html_04        <td style="width: 100px">

11用户控件事件使用delegate_html_04            sex</td>

12用户控件事件使用delegate_html_04        <td style="width: 100px">

13用户控件事件使用delegate_html_04            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>

14用户控件事件使用delegate_html_04    </tr>

15用户控件事件使用delegate_html_04    <tr>

16用户控件事件使用delegate_html_04        <td style="width: 100px">

17用户控件事件使用delegate_html_04        </td>

18用户控件事件使用delegate_html_04        <td style="width: 100px">

19用户控件事件使用delegate_html_04            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="测试" /></td>

20用户控件事件使用delegate_html_04    </tr>

21用户控件事件使用delegate_html_04</table>

22用户控件事件使用delegate_html_04


控件的cs部分

 1用户控件事件使用delegate_html_04using System;

 2用户控件事件使用delegate_html_04using System.Data;

 3用户控件事件使用delegate_html_04using System.Configuration;

 4用户控件事件使用delegate_html_04using System.Collections;

 5用户控件事件使用delegate_html_04using System.Web;

 6用户控件事件使用delegate_html_04using System.Web.Security;

 7用户控件事件使用delegate_html_04using System.Web.UI;

 8用户控件事件使用delegate_html_04using System.Web.UI.WebControls;

 9用户控件事件使用delegate_html_04using System.Web.UI.WebControls.WebParts;

10用户控件事件使用delegate_html_04using System.Web.UI.HtmlControls;

11用户控件事件使用delegate_html_04

12用户控件事件使用delegate_html_04public partial class ctlForm : System.Web.UI.UserControl

13用户控件事件使用delegate_用户控件用户控件事件使用delegate_点击事件_02用户控件事件使用delegate_html_03{

14用户控件事件使用delegate_html_40    protected void Page_Load(object sender, EventArgs e)

15用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

16用户控件事件使用delegate_html_40

17用户控件事件使用delegate_html_45    }

18用户控件事件使用delegate_html_40

19用户控件事件使用delegate_html_40    public delegate void ClickHander();

20用户控件事件使用delegate_html_40

21用户控件事件使用delegate_html_40    public ClickHander MyClickHandler = null;

22用户控件事件使用delegate_html_40

23用户控件事件使用delegate_html_40    public void Button1_Click(object sender, EventArgs e)

24用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

25用户控件事件使用delegate_html_40        if (MyClickHandler != null)

26用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42        用户控件事件使用delegate_html_03{

27用户控件事件使用delegate_html_40            MyClickHandler();

28用户控件事件使用delegate_html_45        }

29用户控件事件使用delegate_html_45    }

30用户控件事件使用delegate_html_62}

31用户控件事件使用delegate_html_04



我们调用这个控件的页面写法如下:

 1用户控件事件使用delegate_html_04<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testForm.aspx.cs" Inherits="testForm" %>

 2用户控件事件使用delegate_html_04

 3用户控件事件使用delegate_html_04<%@ Register Src="ctlForm.ascx" TagName="ctlForm" TagPrefix="uc1" %>

 4用户控件事件使用delegate_html_04

 5用户控件事件使用delegate_html_04<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 6用户控件事件使用delegate_html_04

 7用户控件事件使用delegate_html_04<html xmlns="http://www.w3.org/1999/xhtml" >

 8用户控件事件使用delegate_html_04<head runat="server">

 9用户控件事件使用delegate_html_04    <title>无标题页</title>

10用户控件事件使用delegate_html_04</head>

11用户控件事件使用delegate_html_04<body>

12用户控件事件使用delegate_html_04    <form id="form1" runat="server">

13用户控件事件使用delegate_html_04    <div>

14用户控件事件使用delegate_html_04        <uc1:ctlForm ID="CtlForm1" runat="server" />

15用户控件事件使用delegate_html_04    

16用户控件事件使用delegate_html_04    </div>

17用户控件事件使用delegate_html_04    </form>

18用户控件事件使用delegate_html_04</body>

19用户控件事件使用delegate_html_04</html>

20用户控件事件使用delegate_html_04

调用控件的cs代码如下


 1用户控件事件使用delegate_html_04using System;

 2用户控件事件使用delegate_html_04using System.Data;

 3用户控件事件使用delegate_html_04using System.Configuration;

 4用户控件事件使用delegate_html_04using System.Collections;

 5用户控件事件使用delegate_html_04using System.Web;

 6用户控件事件使用delegate_html_04using System.Web.Security;

 7用户控件事件使用delegate_html_04using System.Web.UI;

 8用户控件事件使用delegate_html_04using System.Web.UI.WebControls;

 9用户控件事件使用delegate_html_04using System.Web.UI.WebControls.WebParts;

10用户控件事件使用delegate_html_04using System.Web.UI.HtmlControls;

11用户控件事件使用delegate_html_04

12用户控件事件使用delegate_html_04public partial class testForm : System.Web.UI.Page

13用户控件事件使用delegate_用户控件用户控件事件使用delegate_点击事件_02用户控件事件使用delegate_html_03{

14用户控件事件使用delegate_html_40    protected void Page_Load(object sender, EventArgs e)

15用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

16用户控件事件使用delegate_html_40        CtlForm1.MyClickHandler = new ctlForm.ClickHander(this.Test);

17用户控件事件使用delegate_html_45    }

18用户控件事件使用delegate_html_40

19用户控件事件使用delegate_html_40    public void Test()

20用户控件事件使用delegate_c#_41用户控件事件使用delegate_控件_42    用户控件事件使用delegate_html_03{

21用户控件事件使用delegate_html_40        Response.Write("ok");

22用户控件事件使用delegate_html_45    }

23用户控件事件使用delegate_html_40

24用户控件事件使用delegate_html_40    

25用户控件事件使用delegate_html_62}