The control mimics the button in the WinForm to support pictures and text. You can choose whether to perform a server-side or a client program, and there are some simple settings.
Insufficient is not support style, next time you want to write a tool bar.
Here's the code.
The following is the referenced content:
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 ("Picture path"), 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 ("Arial", 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 = NE W 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 method"), Category ("Action")]
public string jsfunction
{
get {return this.jsfunction;}
Set {this.jsfunction = value;}
}
Private Raiseeventtype Raiseevent=raiseeventtype.server;
[Browsable (True), Bindable (True), Description ("Response event Mode"), Category ("Action")]
Public Raiseeventtype RaiseEvent
{
get {return this.raiseevent;}
set {this.raiseevent = value;}
}
Private TextAlign align = Textalign.left;
[Browsable (True), Bindable (True), Description ("Text alignment"), Category ("appearance")]
Public TextAlign ALign
{
get {return align;}
set {align = value;}
}
Private Unit width = 80;
[Browsable (True), Bindable (True), Description ("Control width"), 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 ("Picture class"), category ("appearance")]
Public Picture.align Picalign
{
get {return picalign;}
set {picalign = value;}
}
[Browsable (True), Bindable (True), Description ("Picture class"), category ("appearance")]
[DesignerSerializationVisibility (Designerserializationvisibility.content)]
[TypeConverter (typeof (Expandableobjectconverter))]
[PersistenceMode (Persistencemode.innerproperty)]
Public picture Pic
{
get {return pic;}
}
[Browsable (True), Bindable (True), Description ("literal class"), 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). 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 Members
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 Custom Control ImageButton