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>");
}
}
}
}

  

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;
}
}
}

  

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) { }
}
}

  

<%@ 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>

  

最新文章

  1. ZBrush中的头部模型该如何进行雕刻
  2. Html一天入门
  3. c helloworld on zynq
  4. ubuntu server 安装
  5. java rest接口返回不完整的json数据
  6. JS动态增加页面上的控件实例
  7. nginx负载均衡(一)
  8. Kotlin——最详细的数据类型介绍
  9. Java基础总结--流程控制
  10. Java程序员的Golang入门指南(下)
  11. JavaScript的数组知识案例之随机点名器
  12. Revit 插件产品架构梳理
  13. git fork代码并修改胡提交到自己的git仓库
  14. [POI2010]KLO-Blocks——一道值得思考的题
  15. Linux wc
  16. Windows上MyEclipse2017 CI7 安装、破解以及配置
  17. HDU 3435 A new Graph Game(最小费用最大流)&amp;amp;HDU 3488
  18. Jmeter入门(一)————线程组配置
  19. 软工实践-Alpha 冲刺 (4/10)
  20. 【Python】关于使用pycharm遇到只能使用unittest方式运行,无法直接选择Run

热门文章

  1. scala 学习笔记二 方法与函数
  2. CSDN问答频道“华章杯”11月排行榜活动开始,丰厚奖品等你拿
  3. DockerHub基于Github自己主动化构建
  4. 《linux 内核全然剖析》 sys.c 代码分析
  5. 一个用于发送HTML格式邮件的类
  6. leetcode笔记:Sqrt(x)
  7. C#.NET常见问题(FAQ)-如何引用定义好的dll文件
  8. Android 之类库常用包
  9. Android IO存储总结
  10. java面试第十三天