用户控件(UserControl) 使用事件 Ver2

在前一篇中http://www.cnblogs.com/insus/archive/2011/11/16/2251314.html,Insus.NET实作了七个简练在asp.net网页上应用委托与事件的事例。

这一次Insus.NET想重构一下页面上的操作按钮(如下),应用方面如:http://www.cnblogs.com/insus/archive/2011/10/09/2202301.html 或者http://www.cnblogs.com/insus/archive/2011/10/27/2226703.html

图片 1

 

把那么些操作铵钮放在3个UserControl(用户控件)里,页面须要时,接进去即可。这几个用户控件,可参看,每一个Button分别设定CommandName和写同2个OnCommand事件。

图片 2图片 3InsusUserControl.ascx

<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”InsusUserControl.ascx.cs”
    Inherits=”InsusUserControl” %>
<asp:Button ID=”ButtonInsert” runat=”server” Text=”Insert” CommandName=”Insert” OnCommand=”Execute_Command” />
<asp:Button ID=”ButtonEdit” runat=”server” Text=”Edit” CommandName=”Edit” OnCommand=”Execute_Command” />
<asp:Button ID=”ButtonUpdate” runat=”server” Text=”Update” CommandName=”Update” OnCommand=”Execute_Command” />
<asp:Button ID=”ButtonCancel” runat=”server” Text=”Cancel” CommandName=”Cancel” OnCommand=”Execute_Command” />
<asp:Button ID=”ButtonDelete” runat=”server” Text=”Delete” CommandName=”Delete” OnCommand=”Execute_Command” />

 

InsusUserControl.ascx.cs:

图片 4图片 5InsusUserControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class InsusUserControl : System.Web.UI.UserControl
{  
    //揭晓3个事变
    public event CommandEventHandler Execute;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Execute_Command(object sender, CommandEventArgs e)
    {
        if (Execute != null)
        {
            Execute(this, e);
        }
    }
}

 

上面是页面使用这么些用户控件,在aspx在设计格局下,拉那些用户控件到页面中来,当然你也可以在aspx.cs内写代码动态增加:

Default.aspx:

图片 6图片 7View Code

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<%@ Register Src=”InsusUserControl.ascx” TagName=”InsusUserControl” TagPrefix=”uc1″ %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns=”http://www.w3.org/1999/xhtml"&gt;
<head runat=”server”>
    <title></title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <uc1:InsusUserControl ID=”InsusUserControl1″ runat=”server” />
    </div>
    </form>
</body>
</html>

 

Default.aspx.cs:

图片 8图片 9View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.InsusUserControl1.Execute += new CommandEventHandler(InsusUserControl1_Execute);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void InsusUserControl1_Execute(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case “Insert”:
                ShowMessage(e.CommandName);
                break;
            case “Edit”:
                ShowMessage(e.CommandName);
                break;
            case “Update”:
                ShowMessage(e.CommandName);
                break;
            case “Cancel”:
                ShowMessage(e.CommandName);
                break;
            case “Delete”:
                ShowMessage(e.CommandName);
                break;
        }
    }

    private void ShowMessage(string buttonName)
    {
        Response.Write(“你点击了” + buttonName + “铵钮。”);
    }
}

 

动画演示:

图片 10

 

另外动态增加用户控件,能够参考:

http://www.cnblogs.com/insus/articles/2023678.html

http://www.cnblogs.com/insus/articles/1632915.html

http://www.cnblogs.com/insus/articles/2037385.html

 

 

相关文章