控件模仿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. 转:FSMT:文件服务器从03迁移到08R2实战演练

    另外参见:http://www.canway.net/Lists/CanwayOriginalArticels/DispForm.aspx?ID=282 以前做过一个项目,是把文件服务器从03升级到0 ...

  2. LevelDB初体验测试

    最近工作需要找一个能使用磁盘存储数据,对写要求比较苛刻,需要每秒达100000TPS,读的时候需要能10000TPS左右,不能占用太多内存.单节点满足这个要求的常见有Redis.Memcached等, ...

  3. JERSEY中文翻译(第三章、模块和依赖)

    Chapter 2 Modules and Dependencencies 2.1 Java SE 兼容 所有的Jersey组建都是基于Java6开发的,所以你的Java必须是Java6以上的版本才能 ...

  4. RESTful Web服务的操作

    1.首先我们说一下Http协议是无状态的 HTTP协议是无状态的,我们看到查到的用到的返回404,500,200,201,202,301.这些不是HTTP协议的状态码. 是HTTP的状态码,就是HTT ...

  5. 用Putty连接连接Linux

    1:vi打开 [root@gechong]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 2:编辑IP地址 3:重启服务 [root@gechong /] ...

  6. SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis

    一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...

  7. 使用 git post-receive 钩子部署服务端代码

    在 git 中提交服务器源码的时候,如果能够直接更新到测试服务器,并且重启服务使其生效,会节省懒惰的程序员们大量的时间. git 的 Server-side hook (服务端钩子/挂钩)可以用来做件 ...

  8. iWatch应用开发-oc篇

    1.创建项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/di ...

  9. 虚拟机在NAT模式下主机与宿主机的ip配置

    现有如下环境: 宿主机:win7 (IP为100.101.201.20) 虚拟机: SUSE LinuxEterprise 11 64bit (IP为100.101.201.23) 连接模式如下图: ...

  10. mysql字符生命周期

    mysql的字符集,对数据的导入导出很关键,明白自己集在不同阶段不同场景下的意义即为重要! 01.查看mysql当前字符集 show variables like '%cahr%'; 02.用户请求m ...