ASP.NET使用者控制項事件的定義和實踐

來源:互聯網
上載者:User

標籤:class   code   http   tar   ext   com   

假定使用者控制項(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" >    
    </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  的不同跳轉!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.