天天看點

使用者控件事件使用delegate

使用者控件事件使用delegate

1.在我們寫一個使用者控件時需要考慮到重用得問題,如果控件中包含按鈕事件,

我們不可能将點選事件寫到控件裡,而是我們想吧事件處理得過程寫在

調用控件的頁面中,這是該怎麼處理呢?

我的做法時使用delegate來實作這個功能!

具體做法如下:

下面是控件的html部分

 1

使用者控件事件使用delegate
使用者控件事件使用delegate

<%

使用者控件事件使用delegate

@ Control Language="C#" AutoEventWireup="true" CodeFile="ctlForm.ascx.cs" Inherits="ctlForm" %>

 2

使用者控件事件使用delegate

<table>

 3

使用者控件事件使用delegate

    <tr>

 4

使用者控件事件使用delegate

        <td style="width: 100px">

 5

使用者控件事件使用delegate

            name</td>

 6

使用者控件事件使用delegate

 7

使用者控件事件使用delegate

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>

 8

使用者控件事件使用delegate

    </tr>

 9

使用者控件事件使用delegate

10

使用者控件事件使用delegate

11

使用者控件事件使用delegate

            sex</td>

12

使用者控件事件使用delegate

13

使用者控件事件使用delegate

            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>

14

使用者控件事件使用delegate

15

使用者控件事件使用delegate

16

使用者控件事件使用delegate

17

使用者控件事件使用delegate

        </td>

18

使用者控件事件使用delegate

19

使用者控件事件使用delegate

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="測試" /></td>

20

使用者控件事件使用delegate

21

使用者控件事件使用delegate

</table>

22

使用者控件事件使用delegate

控件的cs部分

使用者控件事件使用delegate

using System;

使用者控件事件使用delegate

using System.Data;

使用者控件事件使用delegate

using System.Configuration;

使用者控件事件使用delegate

using System.Collections;

使用者控件事件使用delegate

using System.Web;

使用者控件事件使用delegate

using System.Web.Security;

使用者控件事件使用delegate

using System.Web.UI;

使用者控件事件使用delegate

using System.Web.UI.WebControls;

使用者控件事件使用delegate

using System.Web.UI.WebControls.WebParts;

使用者控件事件使用delegate

using System.Web.UI.HtmlControls;

使用者控件事件使用delegate
使用者控件事件使用delegate

public partial class ctlForm : System.Web.UI.UserControl

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

{

14    protected void Page_Load(object sender, EventArgs e)

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

    }

使用者控件事件使用delegate
使用者控件事件使用delegate

    public delegate void ClickHander();

使用者控件事件使用delegate
使用者控件事件使用delegate

    public ClickHander MyClickHandler = null;

使用者控件事件使用delegate

23

使用者控件事件使用delegate

    public void Button1_Click(object sender, EventArgs e)

24

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

25

使用者控件事件使用delegate

        if (MyClickHandler != null)

26

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

27

使用者控件事件使用delegate

            MyClickHandler();

28

使用者控件事件使用delegate

        }

29

使用者控件事件使用delegate

30

使用者控件事件使用delegate

}

31

使用者控件事件使用delegate

我們調用這個控件的頁面寫法如下:

使用者控件事件使用delegate

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testForm.aspx.cs" Inherits="testForm" %>

使用者控件事件使用delegate
使用者控件事件使用delegate

<%@ Register Src="ctlForm.ascx" TagName="ctlForm" TagPrefix="uc1" %>

使用者控件事件使用delegate
使用者控件事件使用delegate

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

使用者控件事件使用delegate
使用者控件事件使用delegate

<html xmlns="http://www.w3.org/1999/xhtml" >

使用者控件事件使用delegate

<head runat="server">

使用者控件事件使用delegate

    <title>無标題頁</title>

使用者控件事件使用delegate

</head>

使用者控件事件使用delegate

<body>

使用者控件事件使用delegate

    <form id="form1" runat="server">

使用者控件事件使用delegate

    <div>

使用者控件事件使用delegate

        <uc1:ctlForm ID="CtlForm1" runat="server" />

使用者控件事件使用delegate
使用者控件事件使用delegate

    </div>

使用者控件事件使用delegate

    </form>

使用者控件事件使用delegate

</body>

使用者控件事件使用delegate

</html>

使用者控件事件使用delegate

調用控件的cs代碼如下

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

public partial class testForm : System.Web.UI.Page

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

    protected void Page_Load(object sender, EventArgs e)

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

        CtlForm1.MyClickHandler = new ctlForm.ClickHander(this.Test);

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

    public void Test()

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

        Response.Write("ok");

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate

作者:水木    

使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate
使用者控件事件使用delegate