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!