Asp. NET custom controls Getting Started Demo

Source: Internet
Author: User
Tags control label

Recently viewed MSDN on the introduction of custom controls, according to the official documentation, I learned to do a simple demo to the needs of friends reference.

ASP. NET source textbox is without label label, here I want to achieve is to create a label with a TextBox, and the default padding text value is a GUID (read only)

In fact, many third-party components now have such controls, such as Ext.net,fineui and so on. This is just to learn about the development of custom controls.

Step into the chase

1. Create a class library project in VS2010, the frame I chose is 3.5. Name "Mytextboxcontrol"

2. Then create a new class file with the name "MyTextBoxControl.cs"

Using system;using system.collections.generic;using system.linq;using system.text;using System.Web;using System.componentmodel;using system.web.ui;using system.web.ui.webcontrols;using System.Security.Permissions; namespace mytextboxcontrol{[AspNetHostingPermission (SecurityAction.Demand, level = Aspnethostingpermissionl Evel. Minimal), AspNetHostingPermission (SecurityAction.InheritanceDemand, level = Aspnethostingpermissionlevel.minimal ), Defaultproperty ("Text"), ToolBoxData ("<{0}:mytextboxcontrol runat=\" Server\ "> </{0}:mytextboxcontrol&    gt; ")]        public class mytextboxcontrol:textbox//inherit from source textbox {//<summary>///For TextBox Extended label label        </summary> [Bindable (True), Category ("appearance"), DefaultValue ("Label1:"),        Description ("The Control content text."), localizable (true)] public virtual string Label     {get {           string s = (string) viewstate["Label"]; return (s = = null)? "Label1:": s;//if the control Label is empty, the default value is "Label1"} set {viewstate["label"] = Valu            E }}///<summary>//Redraw controls///</summary>//<param name= "writer" >&l t;/param> protected override void Render (System.Web.UI.HtmlTextWriter writer) {System.Web.UI.            Webcontrols.label lable = new Label ();            Lable.id = "Label1"; Lable. Text = this. label;//sets the label text lable. Font.Bold = true;//The set text is displayed in bold lable. RenderControl (writer);//exports the label to the control in this. width = unit.pixel (255);//Set default width this. Text = Guid.NewGuid (). ToString ("N"). ToUpper ();//default text, set default literal value this. ReadOnly = true;//Sets the text default read-only base. Render (writer);//Output Control}}}

Compile the project and generate the MyTextBoxControl.dll file in the Debug directory

3. Create a new Web site project to test the custom control, the project name "TestControl" first refers to the control you just

On the VS2010 menu, choose Tools, select Toolbox, select the directory where MyTextBoxControl.dll is located, and determine

The control that you just added appears on the VS2010 toolbar.

Then you can drag and use the same as other controls, see the following

Here the Label property defaults to NULL, and we can set it to another value. Last previous run diagram

My demo is simple, just getting started. More complex official documents have a very detailed introduction of interested friends can study more http://msdn.microsoft.com/zh-cn/library/bb386519 (v=vs.90). aspx

Demo download

Asp. NET custom controls Getting Started Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.