在前一篇中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