Welcome-Label Custom Web Control in ASP. NET

This article is all about creating a Welcome-Label Custom Web Control in ASP.NET.


Introduction:

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

  • Go to Visual Studio and open File->New->Project->Add ASP.NET Server Control application.

  • Click OK.

ServerControl 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.

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;
using System.Drawing;
namespace ServerControl1
{
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:W_Label1 runat=server></{0}:W_Label1>")]
 public class W_Label1 : Label
 {
  [Bindable(true)]
  [Category("Appearance")]
  [DefaultValue("")]
  [Description("Here text will not see when the user logged in.")]
  [Localizable(true)]
 public string D_name
 {
  get
   {
    String s = (String)ViewState["D_name"];
    return ((s == null) ? "[" + this.ID + "]" : s);
   }
  set
   {
    ViewState["D_name"] = value;
   }
 }

 protected override void RenderContents(HtmlTextWriter output)
 {
   output.Write(Text);
   string ShowName = D_name;
   if (Context != null)
     {
      string userName = Context.User.Identity.Name;
      if (!String.IsNullOrEmpty(userName))
       {
         ShowName = userName;
       }
     }
    if (!String.IsNullOrEmpty(ShowName))
      {
       output.Write(", ");
       output.WriteEncodedText(ShowName);
      }
    output.Write("!");
    }
  }
}

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 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 .cs file

     [assembly: TagPrefix("ServerControl1", "aspSample")]

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.

  • File->New->Web Site

  • Click OK.

Web site application

Step 8: Now we will add a new project

  • Go to Solution Explorer

  • Right Click on it and add existing project

  • Add the ServerControl1 project to the website project.

both application

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 ServerControl1

Step 11: Now the W_Label looks like as into the ServerControl1 Component.

add project reference

Step 12: Now if you want to add it into the Toolbox Control then

  • Right Click on any control of Toolbox.

  • And Select Choose item.

  • Select the .NET Component and browse the button add the ServerControl1.dll to the .net Component.

  • Click OK.

reference of .net component
  • Now the control will appear into the toolbox.

Control in Toolbox

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.

<system.web>
    <
pages>
      <
controls>
        <
add tagPrefix="aspSample" assembly="ServerControl1" namespace="ServerControl1"></add>
      </controls>
    </
pages>
</
system.web>

Step 15: Now we have to drag and drop the W_label Control to the Default.aspx page and press F5

Output :

output