Creating and Using User Controls in ASP.Net
Author: Faraz
Download Source Code : 214_UserControls.zip
In this article you will learn about creating an ASP.Net that contains your own defined controls and adding a code on that page in a code behind.
One of the extra features offered with ASP.Net 1.x is the ability to create you own control. Typically, when you create you own control, you start by encapsulating other controls and then building on that to provide yourself with a solution to a common but complex problem.
For example, you have numerous ASP.Net pages where you ask the visitors for the comments. To add a TextBox control again and again for each page, you can create a User control containing a TextBox Control. Place this control in your ASP.Net page and simply use it where ever you need comments from the visitor on your page.
Firstly, create a web application add Web User Control by right clicking on the project (in the solution explorer window) and add the following code in the HTML design view of the page.
This page contains the three controls; Label control, TextBox control and the Button control. The Label control is used to display title message on the top of the page. The TextBox control is used to add comments from the visitor. And the button control is used to submit those comments.
Note: This file is saved with the .ascx extension.
You can also add the code in the code behind for the .ascx file. For example, you want to display message in the TextBox when the page loads:
Or you want to show comments written by the visitor on the webpage when he/she press the Submit button: