Use user controls in ASP. NET

Source: Internet
Author: User
User Control Overview

User ControlProgramUsers can easily divide and reuse common UI functions across ASP. NET web applications. Like a web form page, a user control can be compiled and stored in the server memory during the first request to shorten the response time of subsequent requests. However, unlike web forms pages, user controls cannot be requested independently. user controls must be included in web forms pages before they can be used.

Create a user control

You can create a user control using a text or HTML editor. The user control declaration syntax is similar to the syntax used to create a web form page; the main difference between the two is that user controls do not include <HTML>, <body>, and <form> elements around the content. These elements are included in the web forms page that contains the user control. User Controls can be as simple as text files, or they can contain other ASP. NET Server controls. The following process briefly introduces a simple logon form that can be included on multiple pages of the application.
[Note] Any ASP. all attributes and methods of the. NET Server Control will be upgraded to the public attributes and methods of the user control.

Use User Controls

When you include this user control on the web form page, you must include the file name and path in the src attribute of the @ register command.

On the web forms page that contains the user control, declare a @ register command, which includes:

Tagprefix attribute, which associates the prefix with the user control. This prefix is included in the start tag of the user control element.

Tagname attribute, which associates the name with the user control. This name will be included in the start tag of the user control element.

Src attribute, which defines the virtual path of the user control file to be included in the web form page.

Example: <% @ register tagprefix = "Acme" tagname = "webusercontrol" src = ". \ webusercontrol. ascx" %>
Usage: <Acme: webusercontrol id = "loginform" runat = "server"/>

User Control usage Summary

User Controls allow developers to easily define custom controls using the same programming skills used to compile web forms pages. 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.

The user control is included in another web form page by using the register command, which specifies the tagprefix, tagname, and SRC location.

After registering a user control, you can mark the user control in the web form page (including the runat = "server" attribute) as a normal Server Control ). 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.

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.

User Controls should not contain any form controls, but should rely on them to contain web forms pages to include form controls when necessary.

You can use the loadcontrol method of the system. Web. UI. Page class to create user controls programmatically. The type of the user control is determined by the ASP. NET Runtime Library and complies with the agreed file name extension.

Only when the register command is included for the user control can the strong type of the user control 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.