用户控件(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

2018正版葡京赌侠诗 1

 

管这些操作铵钮放在一个UserControl(用户控件)里,页面需要常,接进去即可。这个用户控件,可参看,每个Button分别设定CommandName和描写及一个OnCommand事件。

2018正版葡京赌侠诗 22018正版葡京赌侠诗 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:

2018正版葡京赌侠诗 42018正版葡京赌侠诗 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
{  
    //宣告一个事变
    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:

2018正版葡京赌侠诗 62018正版葡京赌侠诗 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:

2018正版葡京赌侠诗 82018正版葡京赌侠诗 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 + “铵钮。”);
    }
}

 

动画片演示:

2018正版葡京赌侠诗 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

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注