ASP.NET自定义Web服务器控件-DropDownList/Select下拉列表控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 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; //自定义web服务器控件-下拉列表 namespace MyControls { [DefaultProperty( "Text" )] [ToolboxData( "<{0}:MySelect runat=server></{0}:MySelect>" )] [ParseChildren( true , "Items" )] //规定 public class MySelect : WebControl { [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; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] //以内容的形式打印标签 public List<SelectItem> Items{ get { if (ViewState[ "Items" ]== null ){ ViewState[ "Items" ] = new List<SelectItem>(); } return (List<SelectItem>)ViewState[ "Items" ]; } set { ViewState[ "Items" ] = value; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] //以内容的形式打印标签 public Size 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" ); //增加样式,宽度 this .Style.Add( "Height" , Size.Height + "px" ); //增加样式,高度 base .RenderBeginTag(writer); } protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Select; //下拉列表标签 } } protected override void RenderContents(HtmlTextWriter output) { //打印下拉列表内容 foreach (SelectItem item in Items){ output.Write( "<option value='" +item.Value+ "'>" +item.Text+ "</option>" ); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; using System.Collections.Generic; using System.Linq; using System.Text; //<!--自定义服务器下拉列表控件的子标签 namespace MyControls { public class SelectItem { public string Value { set ; get ; } //选择的值 public string Text { set ; get ; } //选择的文本内容 public SelectItem() { } public SelectItem( string text, string value) { this .Text = text; this .Value = value; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; using System.Collections.Generic; using System.Linq; using System.Text; //<cc1:MyButton ID="MyButton1" Size-Height="30" Size-Width="290" />用法,用于这种内部属性 //定义一个大小的类,里面包含了宽高 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) { } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <%@ 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" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head runat= "server" > <title></title> </head> <body> <form id= "form1" runat= "server" > <div> <!--自定义服务器下拉列表控件--> <cc1:MySelect Size-Height= "100" Size-Width= "100" ID= "MySelect1" runat= "server" > <cc1:SelectItem Value= "1" Text= "选择一" /> <cc1:SelectItem Value= "2" Text= "选择二" /> <cc1:SelectItem Value= "3" Text= "选择三" /> </cc1:MySelect> </div> </form> </body> </html> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤