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 ...
随机推荐
- 老周发布 UWP 应用的隐私策略(通用)
UWP 应用隐私策略 前注 本声明通用于老周所发布的所有 UWP 应用,下文简称“应用”.开发者全称:周家安,下文简称“老周”. 1.免责声明 您在使用应用过程中,请遵守<中华人民共和国宪法& ...
- Android 之 SharedPreferences应用
Android 平台给我们提供了一个 SharedPreferences 类,它是一个轻量级的存储类,特别适合用于保存共享数据.使用SharedPreferences保存数据,其背后是用xml文件存放 ...
- JAVA 线程池入门事例
线程池这个概念已经深入人心了,今天就是通过几个入门事例,学习一下线程池在JAVA中的应用. 一.大小固定的线程池——Executors.newFixedThreadPool() 下面咱们明确两个类: ...
- Mahout 协同过滤 itemBase RecommenderJob源码分析
来自:http://blog.csdn.net/heyutao007/article/details/8612906 Mahout支持2种 M/R 的jobs实现itemBase的协同过滤 I.Ite ...
- Oracle内存结构:SGA PGA UGA
内存结构是oracle数据库最重要的组成部分之一,在数据库中的操作或多或少都会依赖到内存,是影响数据库性能的重要因素Oracle数据库中包括3个基本的内存结构: 一. 系统全局区 (System G ...
- C++11中的mutex, lock,condition variable实现分析
本文分析的是llvm libc++的实现:http://libcxx.llvm.org/ C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装.不 ...
- 【Linux】ssh建立隧道tunnel连接到内网设备
root@192.168.1.105 建立隧道: ssh -l root -N -f -R 9103:127.0.0.1:2222 work@11.11.13.17 解析:把本地127.0.0.1:2 ...
- ViewPager实现无限循环
引言 这两天在项目里实用到ViewPager来做广告运营位展示.看到如今非常多APP的广告运营位都是无限循环的,所以就研究了一下这个功能的实现. 先看看效果 从一个方向上一直滑动.么有滑到尽头的感觉. ...
- jqPlot图表插件学习之数据节点高亮和光标提示
一.准备工作 首先我们需要到官网下载所需的文件: 官网下载(笔者选择的是jquery.jqplot.1.0.8r1250.zip这个版本) 然后读者需要根据自己的情况新建一个项目并且按照如下的方式加载 ...
- 要想找出以“y”结尾的名字
要想找出以“y”结尾的名字:mysql> SELECT * FROM pet WHERE name LIKE '%y'“_”:匹配任何单个字符“%”:匹配任意数目字符(包括零字符)