国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Asp.net2.0之自定義控件ImageButton

2019-11-17 01:49:04
字體:
來源:轉載
供稿:網(wǎng)友

asp.net2.0之自定義控件ImageButton

控件模仿winform中的button,可以支持圖片和文字??梢赃x擇執(zhí)行服務器端程序還是客戶端程序,還有一些簡單的設置。

不足的是不支持樣式,下次希望可以寫一個工具條。

以下就是代碼

以下為引用的內(nèi)容:

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), Descr

} [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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 佳木斯市| 公安县| 平安县| 闻喜县| 蒲江县| 双桥区| 修武县| 罗源县| 湟中县| 侯马市| 桂阳县| 仁寿县| 宁陕县| 襄汾县| 容城县| 布尔津县| 辽宁省| 蕉岭县| 西宁市| 张北县| 子长县| 喜德县| 梓潼县| 邹平县| 喀喇沁旗| 西吉县| 兴文县| 股票| 双江| 康平县| 深州市| 黄浦区| 涞水县| 白山市| 沁阳市| 建德市| 宁海县| 延津县| 洛南县| 科技| 武安市|