This tutorials explains how we can add custom controls to our project, and how to programmtically access the control’s properties and methods. C# 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:

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

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:
using FreeTextBoxControls;

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:

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

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

Download Source Files