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" >    
    </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  的不同跳转!!

ASP.NET用户控件事件的定义和实践,布布扣,bubuko.com

ASP.NET用户控件事件的定义和实践

上一篇:flask的请求应用


下一篇:Installing Git