ASP.NET用戶控件事件的定義和實踐

假定用戶控件(UserControl.ascx)中包含按鈕控件AButton,希望實現按Button按鈕時,包含該用戶控件的頁面可以接收到事件。

UserControl.ascx.cs中的處理:
1. 定義public的事件委託,如ClickEventHandler;
2. 在UserControl類中聲明事件,如Click;
3. 在UserControl類中定義引發事件的方法,如OnClick()方法;
4. 在UserControl類的相關方法中調用引發事件的方法,如在Button_Click()中調用OnClick()。

 下面這個例子是簡單的響應點擊事件

demo:buttonlist.aspx.cs

using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace stoneControls
{
    public delegate void ClickEventHandler(object sender, EventArgs e);
    public partial class buttonList : System.Web.UI.UserControl
    {
        public event ClickEventHandler Click;
        protected void OnClick(EventArgs e)
        {
            if (Click != null)
                Click(this, e);
        }

        protected void lbnHome_OnClick(object sender, EventArgs e)
        {
            this.OnClick(e);
        }
    }
}

demo:buttonlist.aspx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="buttonList.ascx.cs" Inherits="stoneControls.buttonList" %>
<table>
<tr>
   <td><asp:LinkButton ID="lbnHome" runat="Server" CommandName="HOME" Text="首頁" OnClick="lbnHome_OnClick"></asp:LinkButton></td>
  </tr>
</table>

使用ascx控件

sample:buutonListTest .aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無標題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:buttonList ID="ButtonList1" runat="server" OnClientClick="myclick"/>   
    </div>
    </form>
</body>
</html>

sample:buutonListTest .aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace stoneControls
{
    public partial class buutonListTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ButtonList1.Click+=new ClickEventHandler(ButtonList1_Click);
        }

        protected void ButtonList1_Click(object sender, EventArgs e)
        {

            Response.Write("AAAAAAAAAAAAAAAAAAAAAA");
        }
    }
}

     我們進一步的構造一個帶數據參數的事件,利用.net自帶的commandeventargs,當然可以自己構造一個,去繼承eventargs就行了。

   將上面的委託和事件改改,如下 :

       public delegate void ClickCmandHandler(object sender,CommandEventArgs e);

    public partial class buttonList : System.Web.UI.UserControl
    {
        public event ClickCmandHandler Click;
        protected void OnClick(CommandEventArgs e)
        {
            if (Click != null)
                Click(this, e);
        }

        protected void lbnHome_OnClick(object sender,CommandEventArgs e)
        {
            this.OnClick(e);
        }
    }

   頁面的文件也相應做下修改:

  <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="buttonList.ascx.cs" Inherits="stoneControls.buttonList" %>
<table>
<tr>
   <td>
   <asp:LinkButton ID="lbnHome" runat="Server" CommandName="HOME" Text="首頁" OnCommand="lbnHome_OnClick">
   </asp:LinkButton>  
   </td>
   <td>
   <asp:LinkButton ID="lbnChannel" runat="Server" CommandName="CHANNEL" Text="頻道" OnCommand="lbnHome_OnClick">
   </asp:LinkButton>  
   </td>
   <td>
   <asp:LinkButton ID="lbnColumn" runat="Server" CommandName="COLUMN" Text="欄目" OnCommand="lbnHome_OnClick">
   </asp:LinkButton>  
   </td>
   <td>
   <asp:LinkButton ID="lbnSoft" runat="Server" CommandName="DETAILS" Text="明細" OnCommand="lbnHome_OnClick">
   </asp:LinkButton>  
   </td>
  </tr>
</table>

    調用控件改動下注冊的參數就可以了。。

    protected void Page_Load(object sender, EventArgs e)
        {
            this.ButtonList1.Click+=new ClickCmandHandler(ButtonList1_Click);
        }

        protected void ButtonList1_Click(object sender,CommandEventArgs e)
        {
            if (e.CommandName == "DETAILS")
            {
            }
            if (e.CommandName == "COLUMN")
            {
            }
            if (e.CommandName == "CHANNEL")
            {
            }
            if (e.CommandName == "HOME")
            {
            }
        }

這樣子一個簡單的頁面導航的控件基本出來,根據在commandname的不同跳轉!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章