Use event delegation of ASP. NET user controls effectively

Source: Internet
Author: User

When you are developing an ASP. NET Website, you must be familiar with user controls. When multiple pages use the same or similar design, using user controls can improve development efficiency!

However, you may be troubled by a few different requirements on user controls. For example, the user searcher that is often encountered when processing pages in the development background:

User search is required on multiple pages. Sometimes it is used to modify the user's basic information, sometimes it is used to query the user's order, and sometimes it is used to query the user's Forum information. Multiple pages use the same function searcher. However, when you press the "select" Key, different functions are required. At this time, you can use the user control event to implement this function.

<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"
Onrowcommand = "GridView1_RowCommand">
<Columns>

...................

<Asp: TemplateField>
<ItemTemplate>

<Asp: LinkButton ID = "LinkButton1" runat = "server" CommandName = "Select" CommandArgument = '<% # Eval ("ID") %>'>

Select
</Asp: LinkButton>
</ItemTemplate>
</Asp: TemplateField>

</Columns>

</Asp: GridView>

First, you first set the event object CommandEventHandler in the control. When the GridView1 event is triggered, you can directly call the CommandEventHandler processing method.

Public partial class UserControl: System. Web. UI. UserControl
{

Public event GridViewCommandEventHandler CommandEventHandler;

Protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e)
{
If (CommandEventHandler! = Null)
CommandEventHandler (sender, e );
}

................

}

How to bind the CommandEventHandler of the user control when calling the user control on the page

<Ascx: UserControl ID = "UserControl1" runat = "Server" OnCommandEventHandler = "UserControl_OnCommanEventHandler"/>

Protected void UserControl_OnCommanEventHandler (object sender, GridViewCommandEventArgs e)
{

If (e. CommandName = "Select ")

{

...................
// At this time, you can implement different operations according to different functional requirements.

}
}

To sum up, make proper use of ASP.. NET user control attributes and events can more effectively improve the reusability of user controls, reduce unnecessary code, and improve the efficiency of page development, you can also use custom events for effective development, so that user controls are not restricted by control events!

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.