asp.net2.0 Custom Control ImageButton

Source: Internet
Author: User

Control to mimic the button in WinForm, you can support pictures and text. You can choose whether to execute a server-side program or a client program, and some simple settings.

The disadvantage is not to support the style, the next time you want to write a tool bar.

Here's the code.

The following are the referenced contents:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web.UI.WebControls;
usingSystem.ComponentModel;
usingSystem.Web.UI;
usingSystem.Drawing.Design;
usingSystem.Drawing.Drawing2D;
namespaceClassLibrary1
{
[Serializable]
Public classPicture
{

PrivateUnit Height=  -;

Private stringsrc= 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 stringSRC
{
Get {  return  This. Src;}
Set {  This. SRC=value;}
}
[DefaultValue (
typeof(unit),"16px") ]
[Notifyparentproperty (
true)]
PublicUnit Height
{
Get {  returnheight;}
Set{Height=Value }
}
PrivateUnit Width=  -;
[Notifyparentproperty (
true)]
[DefaultValue (
typeof(unit),"16px")]
PublicUnit Width
{
Get {  returnwidth;}
Set{width=value;}
}

Public enumAlign{left, right}

}
[Serializable]
Public classLabel
{


Private stringtext= string. Empty;
[Notifyparentproperty (
true)]
Public stringText
{
Get {  returntext;}
Set{text=value;}
}
PrivateSystem.Drawing.Font fontFamily=NewSystem.Drawing.Font ("Song Body",8);


[Notifyparentproperty (
true)]
PublicSystem.Drawing.Font Font
{
Get {  return  This. fontFamily;}
Set {  This. fontFamily=value;}
}

}
[PersistChildren (
false)]
[ParseChildren (
true)]
Public  classImagebutton:control, Inamingcontainer,ipostbackeventhandler
{
Public  enumRaiseeventtype {Client,server}
PrivatePicture pic= NewPicture ();
Privatepicture.align picalign=Picture.Align.Left;
PrivateLabel Label= NewLabel ();
Private stringjsfunction= string. Empty;
Private Static ReadOnly ObjectClickkey= New Object();
Public  enumTextAlign {left, center,right}
[Browsable (
true), bindable (true), Description ("JavaScript Method"), Category ("Action")]
Public stringjsfunction
{
Get {  return  This. Jsfunction;}
Set {  This. Jsfunction=value;}
}

PrivateRaiseeventtype RaiseEvent=Raiseeventtype.server;
[Browsable (
true), bindable (true), Description ("Response Event Mode"), Category ("Action")]
PublicRaiseeventtype RaiseEvent
{
Get {  return  This. RaiseEvent; }
Set {  This. RaiseEvent=value;}
}


PrivateTextAlign Align=Textalign.left;
[Browsable (
true), bindable (true), Description ("how text is aligned"), Category ("appearance")]
PublicTextAlign ALign
{
Get {  returnalign;}
Set{Align=value;}

}
PrivateUnit Width=  -;
[Browsable (
true), bindable (true), Description ("Control Width"), Category ("appearance")]
[DefaultValue (
typeof(unit),"80px") ]
PublicUnit Width
{
Get {  return  This. Width;}
Set {  This. Width=value;}
}


[Browsable (
true), bindable (true), Category ("Action")]
Public EventEventHandler OnClick
{
Add
{
Events.addhandler (Clickkey, value);
}
Remove
{
Events.removehandler (Clickkey, value);
}
}
[Browsable (
true), bindable (true), Description ("Picture Class"), Category ("appearance")]
Publicpicture.align Picalign
{
Get {  returnPicalign;}
Set{picalign=Value }
}

[Browsable (
true), bindable (true), Description ("Picture Class"), Category ("appearance")]
[DesignerSerializationVisibility (Designerserializationvisibility.content)]
[TypeConverter (
typeof(Expandableobjectconverter))]
[PersistenceMode (Persistencemode.innerproperty)]
PublicPicture Pic
{
Get {  returnpic;}

}
[Browsable (
true), bindable (true), Description ("Text Class"), Category ("appearance")]
[DesignerSerializationVisibility (Designerserializationvisibility.content)]
[TypeConverter (
typeof(Expandableobjectconverter))]
[PersistenceMode (Persistencemode.innerproperty)]
PublicLabel label
{
Get {  returnLabel }

}
protected Override voidRender (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 voidAddpic (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 voidAddLabel (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>");
       }


#regionIPostBackEventHandler Members

Public voidRaisePostBackEvent (stringeventargument)
{
EventHandler e
=(EventHandler) Events[clickkey];
if(e!=NULL)
{
E (
This, Eventargs.empty);
}
}

#endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.