您的位置:首页 > 编程语言 > ASP

ASP.NET自定义Web服务器控件-Button

2011-08-26 00:00 483 查看
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 MyButton //一个类库,MyButton
{
//(重点1)这两个必须要
[ParseChildren(true)]//将子标签转成属性
[PersistChildren(false)]//

[DefaultProperty("Text")]
[ToolboxData("<{0}:MyButtonCotrol runat=server></{0}:MyButtonCotrol>")]
public class MyButtonCotrol : WebControl,IPostBackEventHandler //继承回发接口
{
[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;
}
}
//(重点2)规定
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]//表示这个属性我们用内部的标签 表示
[TypeConverter(typeof(SizeInfo2Converter))]  //SizeInfo2Converter这个类必须新建
//增加一个属性类似这种( <Size Height="30" Width="100" />如同ListView里面的子控件)
public Size Size
{
get
{
if (ViewState["Size"] == null)
{
ViewState["Size"] = new Size(80, 25); //如果等于null的话就初始化一个Size对象
}
return (Size)ViewState["Size"];
}
set
{
ViewState["Value"] = Size;
}
}
//(重点5)
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input; //转换成input标签
}
}
//(重点3)
protected override void OnInit(EventArgs e)
{
this.Attributes.Add("type","submit");
this.Attributes.Add("value",Text);
this.Attributes.Add("name",this.UniqueID); //回发必须添加的属性
this.Style.Add(HtmlTextWriterStyle.Width,Size.Width+"px");
this.Style.Add(HtmlTextWriterStyle.Height, Size.Height + "px");
base.OnInit(e);
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}

//(重点4)
//定义一个委托
public delegate void ClickHandler(object sender);
private object key; //定义一个键值
public event ClickHandler Click { //定义一个事件
add {
this.Events.AddHandler(key,value);
}
remove {
this.Events.RemoveHandler(key,value);
}
}
//这里触发回发事件
public void RaisePostBackEvent(string eventArgument)
{
ClickHandler handler = (ClickHandler)base.Events[key];
if (handler != null) {
handler(this);
}
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

//给按钮提供的子标签
namespace MyButton
{
//还有一个设计时特性TypeConverter(typeof(ExpandableObjectConverter)),
//它告诉属性浏览器提供扩展和折叠样式,这样控件开发者可以在属性浏览器中直接编辑子属性
[TypeConverter(typeof(ExpandableObjectConverter))]
[Serializable]
public class Size
{
//当子属性发生修改时,.NET框架将自动产生修改通知,并且通知到父属性Size
[NotifyParentProperty(true)]
public int Width { set; get; }
[NotifyParentProperty(true)]
public int Height { set; get; }

public Size(int width,int height) {
this.Width = width;
this.Height = height;
}
public Size() { }
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

//此类是必须要的
namespace MyButton
{
public class SizeInfo2Converter : TypeConverter  //自动转换类一定要继承这个TypeConverter类
{
// 是否能用string转换到SizeInfo2类型。
//判断要转换成Size对象的源是不是字符串(Size<---->标签字符串)
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{ return true; }
else
{ return false; }
}
//判断是否能转换成string类型。
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(Size))  // 如果是Size格式,则允许转成string。
{ return true; }
else
{ return false; }
}

// 从string转到SizeInfo2类型。
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
//如果字符串,不用转了,直接返回一个默点的Size对象
if (value == null || value.ToString().Length == 0) return new Size(70, 30);
//100$30
char spliter = culture.TextInfo.ListSeparator[0];    // 得到字符串的分隔符
string[] ss = ((string)value).Split(spliter);

Int32Converter intConverter = new Int32Converter();   // 得到类型转换器,.net中为我们定义了一些常见的类型转换器。
return new Size((int)intConverter.ConvertFromString(context, culture, ss[0]), (Int32)intConverter.ConvertFromString(context, culture, ss[1]));
}
// 在Property窗口中显示为string类型。
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value == null) return string.Empty;
if (destinationType == typeof(string))
{
Size size = (Size)value;
TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(Int32));   // 能到类型转换器的另一种方式。
char spliter = culture.TextInfo.ListSeparator[0];    // 得到字符串的分隔符
//连接
return string.Join(spliter.ToString(), new string[]{
intConverter.ConvertToString(context, culture, size.Width),
intConverter.ConvertToString(context, culture, size.Height)});
}
return string.Empty;
}
}
}


<!--展示此控件的页面-->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="MyButton" namespace="MyButton" 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:MyButtonCotrol ID="MyButtonCotrol1" Text="提交" runat="server">
<Size Height="30" Width="100" />
</cc1:MyButtonCotrol>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//展示此控件的页面后台
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){

//加载的时候触发一个此控件的点击事件
MyButtonCotrol1.Click += new MyButton.MyButtonCotrol.ClickHandler(MyButtonCotrol1_Click); //加载的时候触发事件
}

void MyButtonCotrol1_Click(object sender)
{
Label1.Text = "触发一个点击事件";
}
protected void Page_PreRender(object sender, EventArgs e)
{
this.DataBind();//绑定此控件
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: