控件模仿winform中的button,可以支持图片和文字。可以选择执行服务器端程序还是客户端程序,还有一些简单的设置。

不足的是不支持样式,下次希望可以写一个工具条。

以下就是代码

以下为引用的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
namespace ClassLibrary1
{
[Serializable]
public class Picture
{

private Unit height = 16;

private string src = string.Empty;
[NotifyParentProperty(true)]
[Browsable(true), Bindable(true), Description("图片路径"), Category("Appearance")]
[Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string Src
{
get { return this.src; }
set { this.src = value; }
}
[DefaultValue(typeof(Unit),"16px") ]
[NotifyParentProperty(true)]
public Unit Height
{
get { return height; }
set { height = value; }
}
private Unit width = 16;
[NotifyParentProperty(true)]
[DefaultValue(typeof(Unit),"16px")]
public Unit Width
{
get { return width; }
set { width = value; }
}

public enum Align{Left ,Right }

}
[Serializable]
public class Label
{

private string text = string.Empty;
[NotifyParentProperty(true)]
public string Text
{
get { return text; }
set { text = value; }
}
private System.Drawing.Font fontFamily=new System.Drawing.Font("宋体",8);

[NotifyParentProperty(true)]
public System.Drawing.Font Font
{
get { return this.fontFamily; }
set { this.fontFamily = value; }
}

}
[PersistChildren(false )]
[ParseChildren(true)]
public class ImageButton:Control ,INamingContainer,IPostBackEventHandler
{
public enum RaiseEventType {Client,Server }
private Picture pic = new Picture();
private Picture.Align picAlign = Picture.Align.Left;
private Label label = new Label ();
private string jsFunction = string.Empty;
private static readonly object clickKey = new object();
public enum TextAlign {Left ,Center,Right }
[Browsable(true), Bindable(true), Description("javascript方法"), Category("Action")]
public string JSFunction
{
get { return this.jsFunction; }
set { this.jsFunction = value; }
}

private RaiseEventType raiseEvent=RaiseEventType.Server ;
[Browsable(true), Bindable(true), Description("响应事件方式"), Category("Action")]
public RaiseEventType RaiseEvent
{
get { return this.raiseEvent; }
set { this.raiseEvent = value; }
}

private TextAlign align = TextAlign.Left;
[Browsable(true), Bindable(true), Description("文字的对齐方式"), Category("Appearance")]
public TextAlign ALign
{
get { return align; }
set { align = value; }

}
private Unit width = 80;
[Browsable(true), Bindable(true), Description("控件宽度"), Category("Appearance")]
[DefaultValue(typeof(Unit),"80px") ]
public Unit Width
{
get { return this.width; }
set { this.width = value; }
}

[Browsable(true),Bindable(true),Category("Action")]
public event EventHandler OnClick
{
add
{
Events.AddHandler(clickKey ,value);
}
remove
{
Events.RemoveHandler(clickKey ,value);
}
}
[Browsable(true), Bindable(true), Descrip(www.111cn.net)tion("图片类"), Category("Appearance")]
public Picture.Align PicAlign
{
get { return picAlign; }
set { picAlign = value; }
}

[Browsable(true),Bindable(true),Description("图片类"),Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content) ]
[TypeConverter(typeof(ExpandableObjectConverter))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public Picture Pic
{
get { return pic; }

}
[Browsable(true),Bindable(true),Description("文字类"),Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content) ]
[TypeConverter(typeof(ExpandableObjectConverter) )]
[PersistenceMode(PersistenceMode.InnerProperty)]
public Label Label
{
get { return label; }

}
protected override void Render(HtmlTextWriter writer)
{
if (raiseEvent == RaiseEventType.Server)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference(this, this.ClientID));
}
else
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick ,"javascript:"+this.jsFunction);
}

writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor ,"hand");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width,this.width.Value.ToString() +"px");
if (align == TextAlign.Left)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign ,"left" );
}
else if (align == TextAlign.Center)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
}
else
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign ,"right");
}

writer.RenderBeginTag(HtmlTextWriterTag.Div );

if (PicAlign == Picture.Align.Left)
{
AddPic(writer);
AddLabel(writer);
}
else
{AddLabel(writer);
AddPic(writer);

}
writer.RenderEndTag();
//base.Render(writer);
}
private void AddPic(HtmlTextWriter writer)
{

writer.AddAttribute(HtmlTextWriterAttribute.Src,base.ResolveClientUrl(pic.Src));
writer.AddAttribute(HtmlTextWriterAttribute.Height ,pic.Height.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.Width ,pic.Width.ToString());

writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
// writer.Write("<image src='"+this.Src+"' height="+pic.Height+" width="+pic.Width+" />");
}
private void AddLabel(HtmlTextWriter writer)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.VerticalAlign,"middle");
writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize ,label.Font.Size.ToString()+"pt");
writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily,label.Font.FontFamily.Name );
if(label.Font.Bold)
{

writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight,"Bold" );
}
writer.RenderBeginTag(HtmlTextWriterTag.Label);

writer.Write(label.Text==string.Empty ?this.ClientID.ToString():label.Text);
writer.RenderEndTag();
//writer.Write("<label>" + Label.Text + "</label>");
}

#region IPostBackEventHandler 成员

public void RaisePostBackEvent(string eventArgument)
{
EventHandler e = (EventHandler)Events[clickKey];
if(e!=null)
{
e(this,EventArgs.Empty );
}
}

#endregion

from:http://www.111cn.net/net/32/9ed732ed9fd1bfde9d3d8bcdddcc7877.htm

Asp.net2.0之自定义控件ImageButton的更多相关文章

  1. ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性

    原文:ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性 深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开 ...

  2. ASP.NET2.0组件控件开发视频 初体验

    原文:ASP.NET2.0组件控件开发视频 初体验 ASP.NET2.0组件控件开发视频 初体验 录了视频,质量不是很好,大家体验下.我会重新录制的 如果不清楚,可以看看http://v.youku. ...

  3. 【IHttpHandler】在ASP.Net2.0中使用UrlRewritingNet实现链接重写

    很多时候我们需要链接转向(Url Rewriting),例如二级域名转向.文章访问链接等场合. 让我们看两个例子: 1 你现在看到的当前作者的博客园的域名: http://jx270.cnblogs. ...

  4. asp.net2.0安全性(2)--用户个性化设置(1)--转载来自车老师

    在Membership表中可以存储一些用户的基本信息,但有的时候,我们需要记录的用户信息远远不止Membership表中提供的这些,如QQ.MSN.家庭住址.联系电话等等.那如何把这些用户信息记录到数 ...

  5. asp.net2.0安全性(2)--用户个性化设置(2)--转载来自车老师

    上一篇我们用Profile.age等方式可以读取用户的年龄和其它的信息,但有的时候我们要查询显示所有用户的信息,但asp.net没有提供查询所有用户信息的功能,我们只能对现有的用户逐一查询其Profi ...

  6. asp.net2.0安全性(3)--验证与授权--转载来自车老师

    "验证"与"授权"是对网页资源安全管理的两道门. 验证(Authentication):检查用户是否是合法的用户.就像是网站大门口的保卫,服责验证使用的用户名和 ...

  7. asp.net2.0安全性(1)--用户角色篇(类)--转载来自车老师

    Membership.MembershipUser和Roles类 用户与角色管理在asp.net2.0中是通过Membership和Roles两个类来实现的. Membership:用户成员账号管理, ...

  8. asp.net2.0安全性(1)--用户角色篇(起篇)--转载来自车老师

    安全管理的解决方案在.net1.1中几乎为一片空白,对于应用程序的验证与授权大部分的工作是开发人员自己编写代码,或者是借助企业库等工具来实现,此可谓.net1.1中的一大缺憾.在.net2.0中微软为 ...

  9. Asp.Net2.0下C#环境 Login控件实现用户登录

    原文:Asp.Net2.0下C#环境 Login控件实现用户登录 一.前台显示效果 二.前台代码             <asp:Login ID="Login1" run ...

随机推荐

  1. Python编程-数据库-利用PyMysql访问windows下的MySql数据库

    1. 下载PyMysql并且安装 下载地址 下载zip包后解压到目录,进入该目录,执行以下命令安装 python setup.py install 2. 编写一个简单的数据库访问程序 simple_m ...

  2. 从C# 2.0新特性到C# 3.5新特性

    一.C# 2.0 新特性: 1.泛型 List<MyObject> obj_list=new List(); obj_list.Add(new MyObject()); 2.部分类(par ...

  3. 第七周 Word文档修订

    come from:http://www.sxszjzx.com/~c20/12-2/office-gj/ 第七周 Word文档修订 教学时间 2013-4-8 教学课时 2 教案序号 12 教学目标 ...

  4. Springmvc之接受请求参数二

    Springmvc之接受请求参数 准备工作 新建一个表单提交 请求地址: http://localhost:8080/ProjectName/user/login.do <form action ...

  5. 转:清理系统垃圾的BAT代码

    @echo off title @echo off color 2 echo. echo. echo 请不要关闭此窗口! echo. echo 开始清理垃圾文件,请稍等...... echo. ech ...

  6. BI Admin Tools和目录管理器的连接配置

    BI管理(Admin Tools): BI Server 的管理工具,用来创建维护模型,并且能够管理安全,会话,变量等等.是用的最多的一个管理工具:这个工具可以编辑 rpd 文件也可以在线通过 ODB ...

  7. Eureka客户端网卡和网段选择

    当机器上有多个网卡或者机器上配置了回环地址的时候,Eureka客户端呈报给服务端的IP将不可预见,为了指定IP我们需要增加以下配置: 在bootstrap.yml中增加配置内容: spring: cl ...

  8. IM系统架构设计之浅见

    转自:http://mobile.51cto.com/hot-439693.htm 背景:除去大名鼎鼎的QQ这款即时聊天工具,还有许多细分行业的IM,比如淘宝阿里旺旺.网易泡泡.YY语音....... ...

  9. webview中事件的用法

    封装 MBProgressHud ==================================== #import "MBProgressHUD.h" @interface ...

  10. LLVM和clang

    LLVM编译器架构 LLVM项目是一套工具的集合,它包括模块化.可复用的编译器及一些列工具链技术. LLVM最开始是Low Level Virtual Machine的简称,但现在它并不是传统意义上的 ...