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

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

    InsusUserControl.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:

    InsusUserControl.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:

    View 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">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <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:

    View 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 + "铵钮。");
        }
    }

    动画演示:

    另外动态添加用户控件,可以参考:

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

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

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

  • 相关阅读:
    [转]UTF-8网页中的头部部分多出一行空白
    php json josn_decode()返回的是对像,如何把对像转成数组
    php file_get_contents计时读取一个文件/页面 防止读取不到内容
    java基础知识 构造方法
    Java基础知识Set、List、Map的区别
    Java基础知识 Set
    java基础语法 List
    java基础语法 数组
    java基础语法this关键字
    http webservice socket的区别
  • 原文地址:https://www.cnblogs.com/insus/p/2253605.html
Copyright © 2020-2023  润新知