user control

This is often the case when doing web development, where the tools available are powerful, but do not meet the needs of a specific project. This is to write your own controls, which are called user controls.

1. What is a user control

Simply put, a user control is a container in which you can place markup and Web server controls. It can be seen as an independent unit, with its own properties and methods, and can be placed on an ASPX page.

Second, create a user control

Creating a user control in VS2010 is very simple, first add a web application, then open "Add New Item" in the added project, select the "Web User Control" template in the dialog box, click the Add button, note that in the After adding to the project, the file extension is .ascx, as shown in Figure 1-1. This extension tells ASP.NET that this file is a user control.


Picture 1-1

First we notice that the source code uses the @Control directive instead of using the @Page directive for standard web pages. Second, unlike standard ASP.NET pages, .ascx files have only one line of code and no other HTML tags. You cannot include <head>/<from> or <body> tags in an .ascx file, because the .ascx file that contains the .ascx file already contains these tags. Figure 1-2


Figure 1-2

3. Using User Controls

In ASP.NET, using user controls is also very simple. Put the user controls on a standard ASP.NET Web page, and directly drag and drop the user control file .ascx from the "Solution Explorer" to the Web page that needs to be used. on the page. Figure 1-3


Figure 1-3

Run the Default page, as shown in Figure 1-4.


Figure 1-4

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325505427&siteId=291194637