asp.net.ph

Skip Navigation Links

Including a User Control in a Web Forms Page

ASP.NET Web Forms   Web Forms Server Controls   Web Forms User Controls


User controls only work when they are included in a Web Forms page. When a request arrives for a page and that page contains a user control, the user control goes through all of the Web Forms Page Processing Stages that any server control is subject to.

Including a user control in a Web Forms page is simple.

To include a user control in a Web Forms page

  1. In the containing Web Forms page, declare an @ Register directive that includes:
    • a tagPrefix attribute, which associates a prefix with the user control. This prefix will be included in opening tag of the user control element.
    • A tagName attribute, which associates a name with the user control. This name will be included in the opening tag of the user control element.
    • A Src attribute, with defines the virtual path to the user control file that you are including in the Web Forms page.

For example, the following code registers a user control defined in the file Login.ascx. It has been given the tag prefix myCtrls and the tag name Login. The file is located in a subdirectory named Controls.

<%@ Register tagPrefix="myCtrls" tagName="Login" src=".\controls\login.ascx" %>
  1. Using custom server control syntax, declare the user control element between the opening and closing tags of an HtmlForm server control ( <form runat=server></form> ) . For example, to declare the control imported in the previous step, use the following syntax.
<html>
<body>
   <form runat="server">
      <myCtrls:Login id="myLogin" runat="server"/>
   </form>
</body>
</html>

NOTE: Regardless of how many ASP.NET server controls ( user controls and any others ) you include on your Web Forms page, you should include only one HtmlForm server control on a Web Forms page. Include all server controls between the opening and closing tags of this control.

See Also

Creating a User Control   Defining Web Forms Event-Handling Methods   Handling User Control Events



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note