Ex Webapp 9
Ex Webapp 9
: 9
Custom Controls
Aim
To develop a website to create a new custom control (NameTextBox)
Procedure
Step -3: Solution Explorer -> Select the Solution WebSite25 (not the website C:\...Website25…\) ->
Press Right Click -> Add-> New Project .
Step -4: Choose Visual c#->Web (from the Installed Templates) ->Select ASP.NET Server Control->click Ok.
Step -5: Go to the Solution Explorer Window and Select the ServerControl1->Press Right Click->Rename as
CustomControls
Step -6: Go to the Solution Explorer Window and Select the ServerControl1.cs->Press Right Click->Rename as
NameTextBox.cs
Step -7: Do the following changes in the coding (NameTextBox.cs). Create a new method IsAlpha() to test the
NameTextBox is alphabet or not.
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 ServerControl1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:NameTextBox runat=server></{0}:NameTextBox>")]
int i;
for (i = 0; i < Text.Length; i++)
{
int av;
av = (int)Text.ElementAt(i);
if (!(((av > 96) && (av < 123)) || ((av > 64) && (av < 91))))
return false;
}
return true;
}
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["Text"] = value;
}
}
Step -8: Click Save and Save All Icon. Select and Right click the CustomControls in the Solution Explorer and
Build the CustomControls
Step -9: Go to Solution Explorer, Select and Right click the website C:\website25…(not the Solution\Website25)
->Add New Item->Web Form.
Step -10: Go to Solution Explorer, Select and Right click the website C:\website25…(not the Solution\Website25)
->Add Reference.
Step -11: Choose the tab “Projects”, Select CustomControls and Press Ok to make Custom control NameTextBox
be available in the ToolBox.
Step -12: Go to the design page of Default.aspx and Design Six Label Controls, One NameTextBox Control (our
new Custom Control) for Student Name and one Textbox control for Employee Name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!NameTextBox1.IsAplpha())
{
NameTextBox1.Text = "";
Label6.Text = "Invalid Student Name, This is NameTextBox , Please Use Alphabets only";
}
else
Label6.Text = "Submitted Successfully";
}
}
Output: