继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

android开发学习视频
关注TA
已关注
手记 304
粉丝 52
获赞 322

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

5acf07320001405b00110016.jpgInsusUserControl.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:

5acf07320001405b00110016.jpgInsusUserControl.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:

5acf07320001405b00110016.jpgView 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:

5acf07320001405b00110016.jpgView 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 + "铵钮。");
    }
}

 

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP