The following describes how to create a user control with an event:
Standard web spaces have events for interaction, and user controls also exist.
To create a user control with an event, you only need three steps:
1. Define a public event Delegate, such as clickeventhandler
2. Define the method for triggering the event in the user control class, such as The onclick method.
3. Check whether the event is empty in the method that triggers the event. If not, write the event.Code.
Next we will implement it step by step,
1. Create a user control file -- linkclick. ascx.
2. Drag the linkbutton control from the toolbox.
<% @ Control Language = " C # " Autoeventwireup = " True " Codebehind = " Linkclick. ascx. CS " Inherits = " Sample9_2.uc.linkclick " %>
< ASP: linkbutton ID = "Linkbutton1" Runat = "Server" Onclick = "Linkbutton#click" > Click me </ ASP: linkbutton >
3. Define the delegate of the click event in the linkclick. ascx. CS post code.
Public event eventhandler myclickeventhandler;
4. The Click Event code for the linkbutton control is as follows:
Protected void linkbutton#click (Object sender, eventargs E)
{
If (myclickeventhandler! = NULL ){
Myclickeventhandler (this, eventargs. Empty );
}
}
5. Drag the user control in default. aspx.
< Uc1: linkclick ID = "Linkclick1" Runat = "Server" />
6. Drag the lable control into the toolbox. We will click the linkbutton of the user control to display the lable content.
< ASP: Label ID = "Label1" Runat = "Server" > </ ASP: Label >
7. Add the user control event handle to the user control,
< Uc1: linkclick ID = "Linkclick1" Runat = "Server" Onmyclickeventhandler = "Linkclickdeleonclick" />
8. open the file, default. aspx. CS, and add the definition code of the event function linkclickjavasonclick.
The Code is as follows (this event is triggered when you click the linkclick control and the corresponding text is displayed in the lablel control ).
Protected void linkclickpoliconclick (Object sender, eventargs E)
{
This. label1.text = "Click me ";
}
The following describes how to create a user control with an event:
Standard web spaces have events for interaction, and user controls also exist.
To create a user control with an event, you only need three steps:
1. Define a public event Delegate, such as clickeventhandler
2. Define the method for triggering the event in the user control class, such as The onclick method.
3. Check whether the event is empty in the method that triggers the event. If it is not empty, you can write the Event code.
Next we will implement it step by step,
1. Create a user control file -- linkclick. ascx.
2. Drag the linkbutton control from the toolbox.
<% @ Control Language = " C # " Autoeventwireup = " True " Codebehind = " Linkclick. ascx. CS " Inherits = " Sample9_2.uc.linkclick " %>
< ASP: linkbutton ID = "Linkbutton1" Runat = "Server" Onclick = "Linkbutton#click" > Click me </ ASP: linkbutton >
3. Define the delegate of the click event in the linkclick. ascx. CS post code.
Public event eventhandler myclickeventhandler;
4. The Click Event code for the linkbutton control is as follows:
Protected void linkbutton#click (Object sender, eventargs E)
{
If (myclickeventhandler! = NULL ){
Myclickeventhandler (this, eventargs. Empty );
}
}
5. Drag the user control in default. aspx.
< Uc1: linkclick ID = "Linkclick1" Runat = "Server" />
6. Drag the lable control into the toolbox. We will click the linkbutton of the user control to display the lable content.
< ASP: Label ID = "Label1" Runat = "Server" > </ ASP: Label >
7. Add the user control event handle to the user control,
< Uc1: linkclick ID = "Linkclick1" Runat = "Server" Onmyclickeventhandler = "Linkclickdeleonclick" />
8. open the file, default. aspx. CS, and add the definition code of the event function linkclickjavasonclick.
The Code is as follows (this event is triggered when you click the linkclick control and the corresponding text is displayed in the lablel control ).
Protected void linkclickpoliconclick (Object sender, eventargs E)
{
This. label1.text = "Click me ";
}