ASP. Ent creates user controls with events

Source: Internet
Author: User

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 ";
}
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.