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;//Custom Web server Control-drop-down list namespace mycontrols{[ Defaultproperty ("Text")] [ToolBoxData ("<{0}:myselect runat=server></{0}:myselect>")] [ParseChildren ( True, "Items")]//Stipulate public class Myselect:webcontrol {[Bindable (True)] [Category ("appearance")] [DefaultValue ("")] [Localizable (true)] public string Text {get {string s = (string) views tate["Text"]; Return ((s = = null)? STRING.EMPTY:S); } set {viewstate["Text"] = value; }} [DesignerSerializationVisibility (designerserializationvisibility.content)]//print the label as content public Li St<selectitem> items{get{if (viewstate["Items"]==null) {viewstate["Items"] = new list<selectitem> (); } return (list<selectitem>) viewstate["Items"; } set{viewstate["Items"] = value; }} [DesignerSerializationVisibility (designerserializationvisibility.content)]//print the label public in the form of content Siz E Size {get {if (viewstate["Size"] = = null) { viewstate["size"] = new Size (); } return (size) viewstate["size"]; } set {viewstate["Size"] = value; }} public override void RenderBeginTag (HtmlTextWriter writer) {this. Style.add ("Width", Size.width + "px"); Add style, Width this. Style.add ("Height", Size.Height + "px"); Add style, height base. RenderBeginTag (writer); } protected override HtmlTextWriterTag Tagkey {get {return htmltextwritertag.select;//drop-down list label}} protected override void RenderContents (HtmlTextWriter output) {//Print drop-down list contents foreach (SelectItem Item in Items) {output. Write ("<option value= '" +item. Value+ "' >" +item. text+ "</option>"); } } }}
Using system;using system.collections.generic;using system.linq;using system.text;//<!-- Custom Server drop-down list control sub-label namespace mycontrols{public class SelectItem {public string value {set; get;}//Selected value Public string Text {set; get;}//Selected text content public SelectItem () {} public SelectItem (string text, String Valu E) {This . Text = text; This. value = value;}}}
Using system;using system.collections.generic;using system.linq;using system.text; <cc1:mybutton id= "MyButton1" size-height= "the" "size-width=" 290 usage, used for this internal attribute//defines a size class, It contains the width of the namespace mycontrols{public class Size {public int width {set; get;} public int Height {set; get;} Public Size (Int. width,int height) {this . width = width; This. Height = height; } Public Size (): This (100,100) {}} }
<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%><%@ Register Assembly= "MyControls" namespace= "MyControls" tagprefix= "CC1"%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
ASP. NET custom Web server control-dropdownlist/select drop-down list control