Partager via


How to: Create ASP.NET User Controls

You create ASP.NET user controls in much the same way that you design ASP.NET Web pages. You can use the same HTML elements and controls on a user control that you do on a standard ASP.NET page. However, the user control does not have html, body, and form elements; and the file name extension must be .ascx.

To create an ASP.NET user control

  1. Open the Web site project to which you want to add user controls. If you do not already have a Web site project, you can create one. For more information, see Local IIS Web Sites or How to: Create File System Web Sites.

  2. On the Website menu, click Add New Item.

    The Add New Item dialog box appears.

  3. In the Add New Item dialog box, under Visual Studio installed templates, click Web User Control.

  4. In the Name box, type a name for the control.

    By default, the .ascx file name extension is appended to the control name that you type.

  5. From the Language list, select the programming language that you want to use.

  6. Optionally, if you want to keep any code for the user control in a separate file, select the Place code in separate file check box.

  7. Click Add.

    The new ASP.NET user control is created and then opened in the designer. The markup for this new control is similar to the markup for an ASP.NET Web page, except that it contains an @ Control directive instead of an @ Page directive, and the user control does not have html, body, and form elements.

    Add any markup and controls to the new user control, and add code for any tasks that the user control will perform, such as handling control events or reading data from a data source.

See Also

Tasks

How to: Include ASP.NET User Controls in Web Pages

Concepts

ASP.NET User Controls Overview