[Component design] How to reference a page object in a component

Source: Internet
Author: User
Sometimes, you may want to write JavaScript code to webform in a certain way in the component, or you may want to mount a method of the component to an event of the page object. At this time, access the Page Object. in <asp. NET Component Design> in his book, Mr. Huang zhongcheng uses the following methods:

1. Create a designer for the component and override the initialize method in the designer, as shown below: Public override initialize (icomponent component)
{
Icontainer Ic = component. Site. container;
Foreach (icomponent C in IC. components)
If (C is page)
(Class Name) Component). _ page = (PAGE) C;
Base. initialize (Component)
}

Mr Huang's method is very effective for components derived from control, webcontrol, and component. The above code principle is to compile a designer for components. In the initialize method of designer, find the page object by traversing the container object of the component, and then specify the Page Object to the public attribute _ page of the page type of the component. Therefore, inside the component, you can reference the _ page attribute. the result of the above practice is that when you drag the designed component into webform, Visual Studio. net designer automatically follows the disgner instructions of the component, traverses the container of the component, finds the page object, and assigns it to the _ page attribute of the component. In fact, it's not that simple. in fact, when dragging components,. net will add the following code to the initializecomponent method in the webform code file: component name. _ page = this;

Initializecomponent this method is available for every webform, which is automatically generated by vs. It is used to initialize various components on the page. When the page is initialized in Init (oninit method ), will call this method, so, in fact, Mr Huang's method is to use. net ide powerful code generation function and component designer to automatically generate initialization component attributes to reference the Page Object (people who have read Mr Huang's book should have strong Print image, because webtimer, webtimer2, almost all webdialog use this method)
In the above method, many people have doubts. First, why does component. container be used instead of component. Site. Container When referencing a component container? In fact, container is the public attribute of the component. If you do this, you will get an nullexception exception. Why? I don't know either. However, in the Chapter "Dancing With ide" in <component design>, I talked about the relationship between three objects: container, component, and site. In fact, component does not directly contact container, or this can be called the reason.
Second, why is the code generated by the designer "component name. _ page = This;" after the component is dragged in? In the webform code, this refers to the page object itself.
The above method is not good, but not intuitive enough.
In fact, it is easier to reference a page object.
First, objects derived from control and webcontrol can be directly referenced by this. Page.
For an object derived from component, you can use code similar to the above class. However, you do not need to implement it in designer or initialize the icontainer Ic = This when dragging. site. container;
Foreach (icomponent C in IC. components)
If (C is page)
This. _ page = (PAGE) C;

It is very simple. After the _ page object is a reference of webform, you can use it.

The above method has been tested and validated. For some code, see Mr. Huang's original article <ASP. NET Component Design book>

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.