The below example is a demonstration of a simple server control which creates a Text Box.
Follow the below steps to create a new server control.
- Open VS.NET 2005.
- In File->New Select the Project submenu.
- In the Dialog window ,Select the Window, and select the WebControlLibrary.
- Select an appropriate path and give a name as TextControl, to create a new server control.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
namespace WebCustomControl1
{
[DefaultProperty('Text')]
[ToolboxData('<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>')]
public class WebCustomControl1 : WebControl
{
public TextBox _text = new TextBox();
[Bindable(true)]
[Category('Appearance')]
[DefaultValue('')]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState['Text'];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState['Text'] = value;
}
}
protected override void CreateChildControls()
{
_text.Width = 200;
_text.Height = 50;
_text.Text = 'Server Control Creation';
this.Controls.Add(_text);
}
protected override void RenderContents(HtmlTextWriter writer)
{
this.EnsureChildControls();
this.RenderChildren(writer);
}
}
}
Here is an article from ftp online: Build an ASP.NET Server Control that explains how to build a ‘Login Control’.
Permalink