Now we are going to discuss how to create a custom web control in ASP.NET. As we know, a custom control is a control made by the user for a specific purpose and later we can use it in our application. In this
article, we are going to see how we create and implement a custom web
control. Now we are going to make a Welcome-Label Custom control which will check whether or not the user is logged on. This web control will inherit from the Label web
control. Further we have to add it to the toolbox items.
Step 1: First Open the Web
server Control application
Step 2: Now you should change
the name of Server Control.cs file to W_Label.cs.
Step 3: Further Toolbox Data attribute for the
W_Label class, change the string "ServerControl1" to "W_Label" in both
places where it occurs.
Step 4: Now write code for
the class named as W_Label.cs which inherits the base class name as Label.
public class W_Label1 : Label
text will not see when the user logged in.")]
String s = (String)ViewState["D_name"];
return ((s == null)
? "[" + this.ID
+ "]" : s);
override void RenderContents(HtmlTextWriter
string ShowName = D_name;
if (Context != null)
string userName =
ShowName = userName;
Code Description: In this code, we
have a class name as W_label.cs which inherits from the Label class and there are
some properties, named as Default property and Toolbox Data shown by default
property of the controls whereas Toolbox Data shows the format of string for an
element. Further, the attributes like as
[Bindable],[Category],[DefaultValue],[Description],[Localizable] are the
attributes for the property defined by name D-name. We have to make an method
named as RenderContents().The
W_Label control writes text to the response stream by
overriding the inherited the RenderContent() method. The parameter that is
passed into the RenderContents() method is an object of type HtmlTextWriter,
which is a class that has methods for rendering HTML. which will used as a login
welcome to the user which is logged in.
Step 5: Now after doing all
that we have to open a file named AssemblyInfo.cs and write the code given
Step 6: Now we have to
build the application by click on build solution and close it.
Step 7: Now we have to
test the application.
Step 8: Now we will add
a new project
step 9: Right click on
Solution Explorer and set as start up project.
Step 10: Now Right
Click on Solution explorer and add reference of the project name as
Step 11: Now the
W_Label looks like as into the ServerControl1 Component.
Step 12: Now if you
want to add it into the Toolbox Control then
Step 13: Now we have to
add the property which is being created, named Text="Welcome" and D_name="User"
which is given below.
<aspSample:W_Label1 ID="Welcome" runat="server" BackColor="#FFCCCC" Text="Welcome" D_name="User" </aspSample:W_Label1>.
Step 14: Now we have to
open the Web.Config file and add this.
tagPrefix="aspSample" assembly="ServerControl1" namespace="ServerControl1"></add>
Step 15: Now we have to
drag and drop the W_label Control to the Default.aspx page and press F5