Udostępnij za pośrednictwem


Web Forms User Controls

In addition to HTML and Web server controls, you can easily create your own custom, reusable controls by using the same techniques you have learned to develop Web Forms pages. These controls are called user controls.

User controls offer you an easy way to partition and reuse common user interface (UI) functionality across your ASP.NET Web applications. Like a Web Forms page, you can author these controls with any text editor, or develop them using code-behind classes. Also, like a Web Forms page, user controls are compiled when first requested and stored in server memory to reduce the response time for subsequent requests. Unlike pages, however, user controls cannot be requested independently; they must be included in a Web Forms page to work. For more information about the relationship between Web Forms pages and user controls, see Converting a Web Forms Page into a User Control.

User controls offer you greater flexibility than server-side includes (SSIs) by accessing the object model support provided by ASP.NET. Rather than simply including the functionality provided by another file, you can program against any properties you declare in the control, just like any other ASP.NET server control.

While you need to choose a single language when authoring a user control, you can include multiple user controls in a single Web Forms page that have been authored in different languages. For example, you can create a user control with Visual Basic that imports data from an XML file and another user control, created with C#, that contains an order form, and include both controls in the same Web Forms page.

Note   When you create a Web application using Visual Studio .NET, all pages and user controls in the application must be in the same programming language.

In addition, you can cache output from a user control independently from the rest of its containing Web Forms page. Called fragment caching, this technique can improve performance for your site if used appropriately. For example, if your user control contains an ASP.NET server control that makes a database request, but the rest of the page contains only literal text and simple code that runs on the server, you can fragment cache the user control to increase your application's performance. For more information, see Caching Portions of an ASP.NET Page.

In This Section

  • ASP.NET Server Controls
    Introduces the four types of server controls and describes how to create and program them in Web Forms pages.
  • Web Forms Pages
    Describes how to create forms-based Web pages that are processed on the server.
  • Caching Portions of an ASP.NET Page
    Discusses the techniques you can use to cache regions of your pages using the @ OutputCache directive and user controls. This technique is useful if you do not want to cache entire pages.
  • UserControl Class
    Describes the events, methods, and properties of the .NET Framework class associated with user controls.