Assuming that the user control (USERCONTROL.ASCX) contains a button control Abutton, the page that contains the user control can receive events when you want to implement the push button.
Processing in UserControl.ascx.cs:
1. Define the event delegate for public, such as ClickEventHandler;
2. Declare an event in the UserControl class, such as Click;
3. Define the method that raises the event in the UserControl class, such as the OnClick () method;
4. Invoke the method that raised the event in the related method of the UserControl class, such as calling OnClick () in Button_Click ().
The following example is a simple response to a click event
Demo:buttonlist.aspx.cs
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Namespace Stonecontrols
{
public delegate void ClickEventHandler (object sender, EventArgs e);
public partial class ButtonList:System.Web.UI.UserControl
{
public event ClickEventHandler Click;
protected void OnClick (EventArgs e)
{
if (Click! = null)
Click (this, e);
}
protected void Lbnhome_onclick (object sender, EventArgs e)
{
This. OnClick (e);
}
}
}
Demo:buttonlist.aspx
<%@ Control language= "C #" autoeventwireup= "true" codebehind= "ButtonList.ascx.cs" inherits= " Stonecontrols.buttonlist "%>
<table>
<tr>
<td><asp:linkbutton id= "Lbnhome" runat= "Server" commandname= "Home" text= "Home" onclick= "Lbnhome_onclick" > </asp:LinkButton></td>
</tr>
</table>
Use ascx Control
Sample:buutonlisttest. aspx
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<uc1:buttonlist id= "ButtonList1" runat= "Server" >
</div>
</form>
</body>
Sample:buutonlisttest. aspx.cs
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Namespace Stonecontrols
{
public partial class BuutonListTest:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
This. Buttonlist1.click+=new ClickEventHandler (Buttonlist1_click);
}
protected void Buttonlist1_click (object sender, EventArgs e)
{
Response.Write ("Aaaaaaaaaaaaaaaaaaaaaa");
}
}
}
We further construct an event with data parameters, using . NET Bring your own CommandEventArgs , of course, you can construct one yourself, to inherit EventArgs on the line.
Change the above delegate and event as follows:
public delegate void Clickcmandhandler (Object Sender,commandeventargs e);
public partial class ButtonList:System.Web.UI.UserControl
{
public event Clickcmandhandler Click;
protected void OnClick (CommandEventArgs e)
{
if (Click! = null)
Click (this, e);
}
protected void Lbnhome_onclick (Object Sender,commandeventargs e)
{
This. OnClick (e);
}
}
The file of the page is also modified accordingly:
<%@ Control language= "C #" autoeventwireup= "true" codebehind= "ButtonList.ascx.cs" inherits= " Stonecontrols.buttonlist "%>
<table>
<tr>
<td>
<asp:linkbutton id= "Lbnhome" runat= "Server" commandname= "Home" text= "Home" oncommand= "Lbnhome_onclick" >
</asp:LinkButton>
</td>
<td>
<asp:linkbutton id= "Lbnchannel" runat= "Server" Commandname= "channel" text= "channel" oncommand= "Lbnhome_onclick" >
</asp:LinkButton>
</td>
<td>
<asp:linkbutton id= "Lbncolumn" runat= "Server" commandname= "column" text= "columns" oncommand= "Lbnhome_onclick" >
</asp:LinkButton>
</td>
<td>
<asp:linkbutton id= "Lbnsoft" runat= "Server" commandname= "Details" text= "Details" oncommand= "Lbnhome_onclick" >
</asp:LinkButton>
</td>
</tr>
</table>
Call the control to change the registered parameters to be able to:
protected void Page_Load (object sender, EventArgs e)
{
This. Buttonlist1.click+=new Clickcmandhandler (Buttonlist1_click);
}
protected void Buttonlist1_click (Object Sender,commandeventargs e)
{
if (E.commandname = = "DETAILS")
{
}
if (E.commandname = = "COLUMN")
{
}
if (E.commandname = = "CHANNEL")
{
}
if (E.commandname = = "HOME")
{
}
}
This way a simple page navigation control basically comes out, according to different jumps in CommandName!!