Thursday, 21 July 2011

How to Use Custom Controls in ASP.NET and VB


This tutorials explains how we can add custom controls to our project, and how to programmtically access the control's properties and methods. VB version.

ASP.NET's built-in controls are very useful, but unfortunately do not provide everything we want. Thankfully, ASP.NET allows us to create custom controls. This tutuorial will focus on how we can implement these custom controls for use in our projects.

We start by adding a Bin folder to our project. To do this, right-click the main project in the Solution Explorer and choose Add ASP.NET Folder > Bin. Once the Bin folder appears in the Solution Explorer, we can right-click this and then choose Add Existing Item. We can then simply browse to the location of the assembly file (.dll or .pdb). In this example, we are using the FreeTextBox control which can be downloaded from www.freetextbox.com and is the no.1 choice for a free HTML editor in ASP.NET Custom Controls. Once we have added the FreeTextBox.dll to our project, we can use it.

We can also add this to our toolbox for dragging & dropping like other controls, by right-clicking a blank area of the toolbox and choosing Choose Items. If FreeTextBox is not in the list, we can click on the Browse button to locate it. Once in the list, make sure it is selected (ticked).

Once you see the icon in the toolbar, drag it onto the page you want it to be displayed. Doing this will add the following to the top of your page:

<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

And you will also see the following where we want the FreeTextBox custom control to be placed:

<FTB:FreeTextBox ID="FreeTextBox1" runat="server">
</FTB:FreeTextBox>

This is all that is needed for us to use the control within our page. However, if we want to interact with the control programmatically, we will have to add the assembly reference to the code-behind:

Imports FreeTextBoxControls

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The above code will allow us to reference the FreeTextBox in our code, and also allow us to access its properties and methods. However, if we are using a naming container such as a MasterPage, we will need to use the FindControl method as well. This will be done by writing code similar to this:

Dim myFreeTextBox As FreeTextBox = CType(Master.FindControl("FreeTextBox1"), FreeTextBox)

Once we have done this, we can access the control as below:

Imports FreeTextBoxControls

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FreeTextBox1.Height = 150
FreeTextBox1.Text = "We are using the <b>Custom Control FreeTextBox</b> and changing its Text attribute programmatically."
End Sub
End Class

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Or if we had to use FindControl, it would look something like this:

Imports FreeTextBoxControls

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myFreeTextBox As FreeTextBox = CType(Master.FindControl("FreeTextBox1"), FreeTextBox)
myFreeTextBox.Height = 150
myFreeTextBox.Text = "We are using the <b>Custom Control FreeTextBox</b> and changing its Text attribute programmatically."
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment