How can I create a server control?

Platform: ASP.NET| Category: Custom Controls

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.

  1. Open VS.NET 2005.
  2. In File->New Select the Project submenu.
  3. In the Dialog window ,Select the Window, and select the WebControlLibrary.
  4. 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’.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.