Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Build the right apps the right way with powerful development tools.
Visual Studio 2010. Learn more...
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.

 

<asp:Label id="lblMsg" Font-Bold="True" Font-Name="Verdana" Font-Size="11pt" runat="server" ForeColor="#6666ff">Add Your Comments about the User Control!asp:Label><br><br>

 

<asp:TextBox id="txtAddComments" TextMode="MultiLine" Font-Name="Verdana" Font-Size="10pt" ForeColor="CornflowerBlue" runat="server" OnFocus="this.value = ''" Height="100" Width="175"/> <br><hr align="left" width="175">

 

<asp:Button id="cmdSubmit" BackColor="#9999cc" ForeColor="#ffffff" runat="server" Text="Submit" /><br><hr align="left" width="175" >

 

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:

 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

    'Put user code to initialize the page here

    If Not IsPostBack Then

      txtAddComments.Text = "Add your Comments Here!"

    End If

 

End Sub

Or you want to show comments written by the visitor on the webpage when he/she press the Submit button:

Private Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click

 

    Response.Write(txtAddComments.Text)

 

  End Sub

These are the simple examples; you can use these events according to you own.

On the .aspx page you simply have to drag and drop the user control from the project. In this way, you can use this control on as many pages as much you want. That is it improves the reusability of the code. When you see the HTML design view for the .aspx file the code will look like this:

 

   <UC:UsrControl id="WUControl11" runat="server" />

Summary:

·         Create a web application and add the web user control by right clicking on the project.

·         Add the code.

·         Drag the user control on the .aspx page and run your application.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Themes and Skin files in ASP.NET 2.0

Disclaimer - Privacy
© 2002-2010 DevASP.net