Asp.net user control usage experience

Source: Internet
Author: User

Asp.net user control 18 hours ago

The purpose of using user controls is to reuse them. We are eager to encapsulate things that often appear on our pages for convenience. From the perspective of using controls, we mainly consider how to transmit data between the home page and user controls. In the initial use, I will make all the methods and attributes of the control public, so that you can modify and call them on the home page at will, but this method is obviously not good, it violates the encapsulation principle of object-oriented design. Although it increases the flexibility of use, it also makes the relationship between pages and user controls complex and increases coupling.

I recently read a book and learned a "regular" usage method and principle. First, you must understand that the page_load method of the page is called before the page_load method of the control. Therefore, do not place the control's initialization control in page_load.

1. Add attributes in the user control class. Add private attributes, and public getter and setter. In this way, you can use the control labels on the home page or use programming methods on the background of the home page. In addition to basic types, user control attributes can also use custom classes.

2. Add events. Sometimes, when an event in the user control is triggered, we hope to obtain some values in the user control on the home page, this involves transferring values from the user control to the home page. Generally, you can define an event in the user control, subscribe to the event on the home page, and pass the value.

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.