Web forms User Control

Source: Internet
Author: User
1. User Control Introduction

In addition to the built-in server controls provided by ASP. NET, you can also easily define your own controls using the same programming skills you have learned to write web forms pages. In fact, you only need to make a few changes, and almost any web form page can be reused as a server control on other pages (note that the user control isSystem. Web. UI. usercontrolType.System. Web. UI. ControlInheritance ). Web form pages used as server controls are referred to as user controls. As agreed, use the. ascx extension to indicate such a control. This ensures that the file of the user control cannot be executed as an independent web form page (you will find that there are few but important differences between the user control and web form page ). User Control throughRegisterCommands are included in the web form page.

<%@ Register TagPrefix="Acme" TagName="Message" Src="pagelet1.ascx" %>

TagprefixDetermine the unique namespace of a user control (so that multiple user controls with the same name can be differentiated ).TagnameIs the unique name of the user control (any name can be selected ).SRCThe attribute is the virtual path of the user control, such as "mypagelet. ascx" or "/MyApp/include/mypagelet. ascx ". After registering a user control, you can mark the user control and place it on a web form page (includingRunat = "server"Attribute ):
<Acme: Message runat = "server"/>

2. Publish user control attributes

When a web form page is regarded as a control, the public fields and methods of the web form are also promoted to the public attributes (marked attributes) and methods of the control.

In addition to promoting public fields to control properties, you can also use the property syntax. Attribute syntax has the advantage of being able to execute code when setting or retrieving attributes.

3. Encapsulate events in user controls

User Controls participate in the entire execution lifetime of the request, similar to common server controls. This means that the user control can process its own events and encapsulate some page logic from web forms pages. Note that the user control itself does not have<Form runat = "server">Control. Only one form control can be displayed on one page (ASP. NET does not allow nested Server Forms). Therefore, a web form page must be included to define the control.

4. Create user controls programmatically

Just as you can create common server controls programmatically, you can also create user controls. PageLoadcontrolThe method is used to load the user control and pass the virtual path of the user control source file:

Control C1 = loadcontrol ("pagelet7.ascx ");
(Pagelet7cs) C1). Category = "business ";
Page. Controls. Add (C1 );

The user control type isControlCommandClassnameAttributes are determined. For example, the user control saved with the file name "pagelet7.ascx" is assigned a strong type "pagelet7cs", as shown below:

<%@ Control ClassName="Pagelet7CS" %>
Because LoadcontrolMethod return System. Web. UI. ControlType, so you must convert it to an appropriate strong type to set the properties of the control. Finally, the user control is added to Controlcollection.

ImportantOnly when the user control is includedRegisterCommand, the strong type of the user control can be used by the page containing web forms (even if there is no actually declared user control flag ).

Summary

  1. User Controls allow developers to easily define custom controls using the same programming skills used to compile web forms pages.
  2. As a convention, the. ascx file extension is used to indicate such a control. This ensures that the user control file cannot be executed as an independent web form page.
  3. User Control throughRegisterCommand is included in another web form page, which specifiesTagprefix,TagnameAndSRC location.
  4. After registering a user control, you can mark the user control in a web form page (includingRunat = "server"Attribute ).
  5. In the web forms page, the public fields, attributes, and methods of the user control are upgraded to the public attributes (tag attributes) and methods of the control.
  6. The user control participates in the entire execution lifetime of each request, and can process its own events and encapsulate some page logic from web forms pages.
  7. User Controls should not contain any form controls, but should rely on them to contain web forms pages to include form controls when necessary.
  8. AvailableSystem. Web. UI. PageClassLoadcontrolMethod To create a user control programmatically. The user control type is determined by the ASP. NET Runtime Library and follows the conventions.File Name_Extension.
  9. Only when the user control is includedRegisterCommand, the strong type of the user control can be used by the page containing web forms (even if there is no actually declared user control flag ).

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.