Introduction:
Here we are going to discuss how to create a custom web control in ASP.NET. The custom control is the control made by the user for a specific purpose and later we can use it in our application. We create a custom control in which the size of text written on the custom server control increases whenever we click on the Button. Further we have to add it to the toolbox items.
Step 1: First Open the Web server Control application
-
Go to visual studio and open File->New->Project->Add ASP.NET Server Control application.
-
Click OK.
Step 2: Now you should change the name of the ServerControl.cs file to IncreaseSize.cs.
Step 3: Further, the toolbox data attribute for the IncreaseSize class, change the string "ServerControl2" to "IncreaseSize" in both places where it occurs.
[ToolboxData("<{0}:IncreaseSize runat=server></{0}:IncreaseSize>")]
Step 4: Now write code in the IncreaseSize.cs class, which inherits the base class name as WebControl.
Code:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Linq;
using
System.Text;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
ServerControl2
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:IncreaseSize
runat=server></{0}:IncreaseSize>")]
public class
IncreaseSize :
WebControl
{
private string
text;
public IncreaseSize()
{
ViewState["IncrSize"]
= "1";
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string
Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null)
? "[" + this.ID
+ "]" : s);
}
set
{
ViewState["Text"]
= value;
}
}
public int
IncrSize
{
get { return
Convert.ToInt32((string)ViewState["IncrSize"]);
}
set { ViewState["IncrSize"]
= value.ToString(); }
}
protected override
void RenderContents(HtmlTextWriter
output)
{
output.Write("<font
size = " + IncrSize + ">" + Text +
"</font>");
}
}
}
Description: Now, first of all, we have to make a constructor of a class to
store the update size in a view state of a control in which the default value
held by view state is 1. On each click, the size of the control increases at that
will store into the constructor which will give us only the updated value.
Further we have to create a property name as IncrSize which must maintain it's
state through the post backs occurs during pressing button.
The property Get method retrieves the value from
ViewState, casts it to a string in the case of C#, and then converts that string
to its integer equivalent. The property Set method stashes a string representing
the size into ViewState. At last we have a method name as RenderContents() which
is declared in the base class named as Web Control and will override into
the derived class named as IncreaseSize which will inherit it. It contains a
parameter of type HTML named as HtmlTextWriter. The
HtmlTextWriter
class derives from TextWriter and provides rich formatting capabilities.
HtmlTextWriter
will ensure that the elements produced are well-formed, and it will manage the
attributes, including style attributes.
Step 5: Now after doing all that we have to open a file named AssemblyInfo.cs and write the code given below.
-
Firstly add a namespace to the Top of the AssemblyInfo.cs file named as
using System.Web.UI;
-
Now write the assembly name at the end of the AssemblyInfo.cs file.






Join the conversation! Your thoughts help the community grow.