Creating ASP.NET User Controls

We create user controls much the same way that we create normal Web forms in ASP.NET; however, we normally denote that a file is to be used as a control by saving it with an .ascx file extension. Let's look at an example of a simple user control.

<asp:label text="Welcome to ASP.NET User Controls" runat="Server"/>

Yes, that's it. That's our entire control. Note that it's nothing more really than a Web form fragment, and in our control definition file we do not include <html>, <head>, <body>, or <form runat="server"> tags. Listing 9.1 shows how we use our control on a Web form.

Listing 9.1. Implementing the User Control on a Web Form (91.aspx)
 <%@ Page language="c#"%> <%@ Register TagPrefix="ASPNETByExample" TagName="Welcome" ...

Get ASP.NET by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.