Summary
This article will explain in detail the process of creating a simple custom server control. With this content, readers will learn the basics of creating and testing custom server controls with Visual Studio 2005.
Note: This article is based on ASP.net 2.0 technology, and the sample application is developed using Visual Studio 2005.
To create a simple custom server control
The process of creating custom server controls includes:
(1) Create a test Web site application;
(2) to create a new site application, add a Web Control library project;
(3) write, compile, and test the custom server control.
(1) Create a test Web site application
With Visual Studio 2005, there are many ways to create a Web site application, such as creating in a local file system, creating with IIS support, creating in an FTP site, creating at a remote site, and so on. However, in any case, the developer should open Visual Studio 2005 First, and then click the New Web site ... item under the File menu. At this point, a dialog box will pop up in Figure 1 below.
Figure 1 New Web Site dialog box
Figure 1 is the new Web Site dialog box. In this dialog box, developers need to choose to create templates, locations, and programming languages. As shown in Figure 1, the sample is created using the ASP.net Web site template, the file system, and the C # programming language. When you click the OK button, Visual Studio 2005 creates a Test1 folder under the native D:\AppTest. All application files are stored in the folder. By default, the Test1 folder includes an empty App_Data folder for storing application data files, including a default.aspx and Default.aspx.cs file, respectively.
A Web site application is created by using the steps above. This web site will be used to test the Web custom server controls that you create.
(2) New custom server control project
After the Web site application is created, the developer must also add a custom server control project, the Web Control Library project, in the current site project. When you open the Web site application, click New Project by clicking the subkey of the Add item on the File menu. At this point, Visual Studio 2005 pops up a dialog box like Figure 2.
Figure 2 Adding a new Project dialog box
Figure 2 is a screenshot of the Add New Item dialog box. The dialog box is divided into three settings: project type, template, name, and location.
There is a tree list on the left side of the dialog box, which includes various item types. To create a Web Control library project, you should select the child node Windows for the Visual C # node. The installed template for the child node appears to the right of the dialog box, including Windows applications, class libraries, Web control libraries, and so on. The developer should select the Web Control Gallery item. Finally, you need to set the name and location of the Web Control Library project. For ease of administration, it is recommended that the Web Control Library project and the test site project be stored in the same folder, so this example sets the name to "Hellomycontrol" and sets the location to D:\AppTest\test1. When you click the OK button, Visual Studio 2005 will automatically create a Hellomycontrol to store the Web Control library related files in the D:\AppTest\test1 directory. Also, the Solution Explorer for Visual Studio 2005 will display the contents shown in Figure 3.
Figure 3 Solution Explorer
As shown in Figure 3, Solution Explorer includes two projects. One is the Web site project created earlier, and the other is a Web Control library project named Hellomycontrol, where the default includes a WebCustomControl1.cs file. You can begin to write, compile, and test your custom server controls.