Definitions and practices of ASP. NET user control events

Source: Internet
Author: User

Assume that the user control (UserControl. ascx) contains the Button control AButton. If you want to press the Button, the page containing the user control can receive the event.

Processing in UserControl. ascx. cs:
1. Define public event delegation, such as ClickEventHandler;
2. Declare events in the UserControl class, such as Click;
3. Define the method for triggering the event in the UserControl class, such as The OnClick () method;
4. Call the method that triggers the event in the related methods of the UserControl class. For example, call OnClick () in Button_Click ().

 The following example shows 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 = "Homepage" OnClick = "lbnHome_OnClick"> </asp: linkButton> </td>
</Tr>
</Table>

UseAscxWidget

Sample: buutonListTest. aspx

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Uc1: buttonList ID = "ButtonList1" runat = "server">
</Div>
</Form>
</Body>
</Html>

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 (buttonlist#click );
}

Protected void buttonlist#click (object sender, EventArgs e)
{

Response. Write ("AAAAAAAAAAAAAAAAAAAAAA ");
}
}
}

We further construct an event with data parameters and use. NetBuilt-inCommandeventargsOf course, you can construct one by yourself to inheritEventargsThat's all.

Modify 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 );
}
}

Modify the page file 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 = "topic" 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>

You can call the control to modify the registered parameters ..

Protected void Page_Load (object sender, EventArgs e)
{
This. ButtonList1.Click + = new ClickCmandHandler (buttonlist#click );
}

Protected void buttonlist#click (object sender, CommandEventArgs e)
{
If (e. CommandName = "DETAILS ")
{
}
If (e. CommandName = "COLUMN ")
{
}
If (e. CommandName = "CHANNEL ")
{
}
If (e. CommandName = "HOME ")
{
}
}

In this way, a simple page navigation control is basically displayed, jump according to the commandname !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.