0% found this document useful (0 votes)
10 views10 pages

Ex Webapp 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Ex Webapp 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Ex. No.

: 9
Custom Controls

Aim
To develop a website to create a new custom control (NameTextBox)

Procedure

Step-1: Go to Microsoft Visual Studio


Step-2: Choose File Menu->New->Website

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>")]

public class NameTextBox : System.Web.UI.WebControls.TextBox


{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

// Creating a new method to NameTextBox

public bool IsAplpha()


{

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;
}
}

protected override void RenderContents(HtmlTextWriter output)


{
output.Write(Text);
}
}
}

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.

Step -13: Write the following code in Button1_Click()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{

}
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";
}
}

Step -14: Press Save and Save All->Build->Start Debugging(Run).

Output:

You might also like