How to Use a Custom Web Control in VS.NET?

In .NET applications, you can use two different controls in web forms:

• User Controls include static HTML Code and associated web controls. It is reusable across pages within an application and you can define your own events, properties and methods

• Custom Web Controls contain precompiled classes which generate HTML Code automatically. You can decide on the appearance, behavior, features and implementation of the control. You can reuse it across applications.



This article will give you an overview on how to use custom web controls in your web application with the help of VS.NET editor.

Before knowing about how to use custom controls, the initial step is to create a custom web control if it does not exist. You can derive your control from System.Web.UI.Control. If you want to implement standard styles like Font and Backcolor, you can better inherit it from System.Web.UI.WebControls.WebControl. Create your control and place it in a new assembly so that you can reuse it in other applications as well. Now to use the custom web control in your application, follow the steps given below:

Step 1: Install the Custom Web Control

Install custom web control in the system in which you perform the development activities. If your control requires a custom designer then ensure that the designer is also installed.

Step 2: Add Custom Web Control to Toolbox

As already instructed, create your custom control in a separate assembly and use Visual Studio 2005’s in-built toolbox support for custom controls. Create a custom control project by choosing the menu File ? Add ? New Project. Define your controls in that project. Now you can use it in your application in two different ways which are mentioned below:

• In the solution explorer right click on your website ? click on “Add Reference” option ? Go to Project tab ? choose the custom control project you have created ? Click OK

• Compile your project which contains the controls, VS will add all your controls automatically into a temporary section of the toolbox. You can now drag and drop the control from the toolbox to your page. But ensure that your custom control project is already loaded in the design environment.

Step 3: Use Custom Web Control

Now the control is visible in the toolbox. How do you use it in your web form? You can manually add the control to your web form by typing the corresponding tag of the control in HTML View. Other easy way is to drag the control from the toolbox and drop it into your web form. What happens when you do this drag and drop?

• Registration of TagPrefix and namespace happens
• Page is added with the new tag generated for this custom web control
• Project is added up with a reference to the custom web control’s assembly
• If any designer is associated with the control then the designer is automatically loaded into the page
• The control appears in the design view

If you are typing the tag in HTML view, then the above mentioned tasks will not happen automatically. You can register the tag prefix and namespace by including the following register directive in the page where you want to access the control:

<%@ Register TagPrefix=”testTagPrefix” Namespace=”CustomServerControlsLibrary” Assembly=”CustomServerControlsLibrary” %>

If you want to use the same control across pages in your web application, then do the following configuration in web.config file:
<system.web>
<pages>
<controls>
<add tagPrefix=”testTagPrefix” namespace=”CustomServerControlsLibrary”
assembly=”CustomServerControlsLibrary” />
</controls>
</pages>
</system.web>

After registering the control, you can declare it as a standard control tag and start using it:
<testTagPrefix:ControlName id=”ControlId” runat=”server” />

Step 4: Deploy the Custom Web Control

The control has to be deployed along with the project. But you need not perform any specific tasks regarding this deployment. It will happen as part of the build/deploy activity.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| Understanding ASP.NET Page Lifecycle | Exploring Different Stages of Memory Management in .NET | How to Implement Forms Authentication Provider in .NET | How to Implement Toolbox Support in .NET? | How to Read and Write Files with Streams in ASP.NET? | How to Use a Custom Web Control in VS.NET? | Implementing .NET Passport Authentication in Web Applications Using Passport Authentication Provider | Using Atlas Architecture in ASP.NET |

 


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.