Asp.net2.0之自定义控件ImageButton
控件模仿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的更多相关文章
- ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性
原文:ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性 深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开 ...
- ASP.NET2.0组件控件开发视频 初体验
原文:ASP.NET2.0组件控件开发视频 初体验 ASP.NET2.0组件控件开发视频 初体验 录了视频,质量不是很好,大家体验下.我会重新录制的 如果不清楚,可以看看http://v.youku. ...
- 【IHttpHandler】在ASP.Net2.0中使用UrlRewritingNet实现链接重写
很多时候我们需要链接转向(Url Rewriting),例如二级域名转向.文章访问链接等场合. 让我们看两个例子: 1 你现在看到的当前作者的博客园的域名: http://jx270.cnblogs. ...
- asp.net2.0安全性(2)--用户个性化设置(1)--转载来自车老师
在Membership表中可以存储一些用户的基本信息,但有的时候,我们需要记录的用户信息远远不止Membership表中提供的这些,如QQ.MSN.家庭住址.联系电话等等.那如何把这些用户信息记录到数 ...
- asp.net2.0安全性(2)--用户个性化设置(2)--转载来自车老师
上一篇我们用Profile.age等方式可以读取用户的年龄和其它的信息,但有的时候我们要查询显示所有用户的信息,但asp.net没有提供查询所有用户信息的功能,我们只能对现有的用户逐一查询其Profi ...
- asp.net2.0安全性(3)--验证与授权--转载来自车老师
"验证"与"授权"是对网页资源安全管理的两道门. 验证(Authentication):检查用户是否是合法的用户.就像是网站大门口的保卫,服责验证使用的用户名和 ...
- asp.net2.0安全性(1)--用户角色篇(类)--转载来自车老师
Membership.MembershipUser和Roles类 用户与角色管理在asp.net2.0中是通过Membership和Roles两个类来实现的. Membership:用户成员账号管理, ...
- asp.net2.0安全性(1)--用户角色篇(起篇)--转载来自车老师
安全管理的解决方案在.net1.1中几乎为一片空白,对于应用程序的验证与授权大部分的工作是开发人员自己编写代码,或者是借助企业库等工具来实现,此可谓.net1.1中的一大缺憾.在.net2.0中微软为 ...
- Asp.Net2.0下C#环境 Login控件实现用户登录
原文:Asp.Net2.0下C#环境 Login控件实现用户登录 一.前台显示效果 二.前台代码 <asp:Login ID="Login1" run ...
随机推荐
- VMware vCloud Director network ports diagram
see:http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&exter ...
- eclipse 运行简单JAVA程序事例
开发JAVA已经有一段时间了,不过要运行JAVA程序,还是在命令行敲命令,很不方便,很麻烦,突然想到eclipse应该也有这个功能,于是百度了一下,将步骤晒出来,供大家参考. 1.创建JAVA工程 单 ...
- cocos2d-js Shader系列4:Shader、GLProgram在jsb(native、手机)和html5之间的兼容问题。cocos2d-js框架各种坑。
为了让jsb也能顺利跑起滤镜效果,在手机侧折腾了2天,因为每次在真机上运行总要耗那么半分钟,而且偶尔还遇到apk文件无法删除导致运行失败的情况. 这个调试起来,实在让人烦躁加沮丧. 还好,测试上百轮, ...
- cocos2d-js 3.0 RC0 监听返回键、菜单键、进入后台(home键)、恢复显示等事件
cc.eventManager.addListener({ event: cc.EventListener.KEYBOARD, onKeyReleased: function(keyCode, eve ...
- Android 虚拟现实(virtual reality)入门指南
入门指南 本文档介绍怎样使用实验性的 Cardboard SDK for Android 创建您自己的虚拟实境 (VR) 体验. Android 演示版应用:Treasure Hunt 本教程中的代码 ...
- python之模块contextlib 加强with语句而存在
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块contextlib,为加强with语句而存在 #特别注意:python3和python2 ...
- python之模块配置文件ConfigParser(在python3中变化较大)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ConfigParser(在python3中为configparser) #特别注意:py ...
- 防火墙 Firewalld
RHEL7支持的防火墙: IPTABLES IP6TABLES FIREWALL EBTABLES 命令模式: firewall-cmd 图形化界面: firewall-config Firewall ...
- LVM逻辑卷管理测试——逻辑卷扩展、收缩、快照及删除
一.逻辑卷扩展 [root@lxjtest /]# umount /testLVM/ [root@lxjtest /]# df -h Filesystem Size Used Avail Use% M ...
- 适配新路由3(D2)的LEDE/OpenWrt固件
使用MediaTek系列的芯片方案 Y1(R6830): MT7620A + MT7612EN(5G 866M) + winbond 25Q128FVFG + winbond W971GG6KB-25 ...