Use delegation to implement interaction between user controls and parent pages

Source: Internet
Author: User
Http://hi.baidu.com/isbx/blog/item/85df08f7ca0fb12a730eec06.html Web User ControlThe control must be associated with its parent page. Interaction. InteractionThere are many different types. Viewstate, session, and even static variables can be used. However, sometimes, when the control triggers a return, the event must be used for processing. Speaking of events, it does not mean that the Web User ControlInternally, these buttons or linkbuttons are used to trigger a callback event. User ControlAs a whole object, it has its own attributes, methods, and events. When User ControlWhen embedded into a page file, it is hidden internally. However, it opens its own attributes, methods, and events. When an internal callback is triggered, it varies depending on the status of the callback, it determines whether to send this back to the parent page of the notification. At the same time, the parent page can know what information the child control can provide.
With the event, you can User ControlAll the information is transmitted to the parent page through eventargs, and then the parent page is processed based on the information. First, we will define an eventargs class. Assume that User ControlContains three child controls, two text boxes, and one button. This button triggers the return event of the control. After the control is returned, it should tell the parent page that it can provide the content of the two text boxes, such as username and password. Create a class file in the business logic layer and create the usercontroleventtest class. Namespace sysu. XK
{
Publicclass usercontroleventtest: eventargs
{
Publicstring username;
Publicstring password;

Public usercontroleventtest (string username, string password)
{
This. Username = username;
This. Password = password;
}
}
} Then User ControlThe code file defines a delegate and adds the event, and then fires the event after the button is clicked:
Using sysu. XK;

Public partial class webusercontrolevent: system. Web. UI. usercontrol
{
Publicdelegatevoid submituserhandler (Object sender, usercontroleventtest E );
Publicevent submituserhandler submituserevent;

Protectedvoid page_load (Object sender, eventargs E)
{

}
Protectedvoid button#click (Object sender, eventargs E)
{
Usercontroleventtest = new usercontroleventtest (textbox1.text, textbox2.text );
Submituserevent (this, usercontroleventtest );
}
}

In this way,User ControlAdd a submit event and handle it: <% @ register tagprefix = "usercontrol" tagname = "Event" src = "~ /Webusercontrolevent. ascx "%>




<Usercontrol: Event id = "event1" runat = "server" onsubmituserevent = "event1_submit"> </usercontrol: Event>

Protectedvoid event1_submit (Object sender, usercontroleventtest E)
{
String username = E. Username;
String Password = E. Password;

Label1.text = username;
Label2.text = password;
} The effect is as follows:

Reference: http://www.cnblogs.com/Freedream/archive/2006/06/26/435786.html
Download Code: http: // 202.116.83.77/hope/sites/XK/share/uctest.rar

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.