Click here to Skip to main content
Licence 
First Posted 9 May 2005
Views 31,630
Bookmarked 24 times

ExtendedImageButton: Image button with extended functionality including - JavaScript rollovers, confirmation pop-up box, disabled state and alt tags

By | 9 May 2005 | Article
This is a fully contained image button control that when used will give the designer clean tri-state rollover. This control also contains an optional confirm pop-up, alt tag and disabled state. This control can act as a simple link (direct to new page) or use its custom event to initiate a postback.

Introduction

I built this control to make my life easier. It seamed like I was always creating roll-over images and buttons with confirm pop-up. I finally got sick of dealing with all the JavaScript snippets. This control is a solution to all my problems. (and hopefully yours;).)

Special note: Thanks to the following two articles on CodeProject. They helped me a lot. (plus I sampled parts of my JavaScript from them.)

Using the code

The following is an example of the code needed to use the control. First reference the CustomControls.dll, then add it to your Toolbox. Next drag a new instance of the ExtendedImageButton control onto the page and then set the properties/events you might need.

protected CustomControls.ExtendedImageButton eibPostBack;
private void Page_Load(object sender, System.EventArgs e) {
    this.eibPostBack.ButtonClick += 
          new System.EventHandler(this.eibPostBack_ButtonClick);
}
private void eibPostBack_ButtonClick(object sender, System.EventArgs e){
    Response.Write("Button Was Clicked");
}

ExtendedImageButtonControl

This is the main code of the control. The Render method is overridden. The code creates an [<A>] tag that is wrapped around an [<img>]tag. The JavaScript necessary for this is added in another method that we'll review in a minute:

protected override void Render(HtmlTextWriter output) {
    if (this.Enabled){
        output.WriteBeginTag("A");
        if (_redirectURL != String.Empty)
            output.WriteAttribute("href", _redirectURL);
        else 
            output.WriteAttribute("href", "javascript:" + 
               this.Page.GetPostBackEventReference(this, "ButtonClick"));
        if (_confirmClick)
            output.WriteAttribute("onclick", 
                      "javascript:return __doConfirm(this);");
        output.Write(">");
    }

    output.WriteBeginTag("img");
    if ((!this.Enabled) && (_imageDisabled != String.Empty)){
        output.WriteAttribute("src", _imageDisabled);
    }else{
        output.WriteAttribute("src", _imageNormal);
    }
    output.WriteAttribute("name", this.UniqueID);

    if (! this.BorderWidth.IsEmpty)
        output.WriteAttribute("border", this.BorderWidth.ToString());
    else
        output.WriteAttribute("border", "0");
    if (! this.BorderColor.IsEmpty)
        output.WriteAttribute("bordercolor", 
                            this.BorderColor.ToArgb().ToString());
    if (this.Style.Count > 0) {
    IEnumerator keys = this.Style.Keys.GetEnumerator();
        string sStyles="";
        while (keys.MoveNext()) {
            sStyles += (string)keys.Current + ":" + 
               this.Style[(string)keys.Current ].ToString() + ";";
        }
        output.WriteAttribute("style", sStyles);
    }
    if (this.CssClass != String.Empty)
        output.WriteAttribute("class", this.CssClass);
    if (! this.Height.IsEmpty)
        output.WriteAttribute("Height", this.Height.ToString());
    if (! this.Width.IsEmpty)
        output.WriteAttribute("Width", this.Width.ToString());
    if (this._allText != String.Empty)
        output.WriteAttribute("alt", _allText);
    if (this.Enabled){
        if (this._imageOnMouseOver != String.Empty)
            output.WriteAttribute("OnMouseOver", "javascript:newRollOver('" + 
               this.UniqueID + "', '" + ImageOnMouseOver + "')");
        if (this._imageNormal != String.Empty)
            output.WriteAttribute("OnMouseOut", "javascript:newRollOver('" + 
               this.UniqueID + "', '" + ImageNormal + "')");
        if (this._imageOnMouseClick != String.Empty)
            output.WriteAttribute("OnMouseDown", "javascript:newRollOver('" + 
               this.UniqueID + "', '" + ImageOnMouseClick + "')");
    }
    output.Write("/>");

    if (this.Enabled){
        output.WriteEndTag("A");
    }
}

The OnInit method is overridden. This method adds the necessary JavaScript methods to the page:

protected override void OnInit(EventArgs e){
    const string RegistrationNameImageSwap = 
                                      "ExtendedImageButton_JSwap";
    const string RegistrationNameConfirmMessage = 
                                     "ExtendedImageButton_JConfirm";
    if (!this.Page.IsClientScriptBlockRegistered(RegistrationNameImageSwap)){
        const string sJSwapCode = 
            "<script language="'javascript'" type='text/javascript'>" +
            "<!-- \n" + 
            "function newRollOver(targetDOMUrlName, URL) { \n" + 
            "//alert(targetDOMUrlName + ' ' + URL); \n " + 
            "var img=document.images; \n " + 
            "var i=0; \n " + 
            "for (i=0; i<img.length; i++) \n " + 
            "if (img[i].name == targetDOMUrlName) img[i].src = URL; \n " + 
            "} \n " + 
            "//-->\n " + 
            "</script>\n ";
        this.Page.RegisterClientScriptBlock(RegistrationNameImageSwap,
                                                             sJSwapCode);
    }
    
    if (_confirmClick) {
       if (! this.Page.IsClientScriptBlockRegistered(
                                            RegistrationNameConfirmMessage))
       {
          string sJSConfirmCode = 
                "<script language="'javascript'" type='text/javascript'>" +
                "<!-- \n" +
                "function __doConfirm(btnWaiter) { \n" +
                "if (confirm('" + _confirmMessage + "')) { \n" +
                "document.body.style.cursor=\"wait\"; \n" +
                "return true; \n" +
                "} return false; } \n" +
                "//--> \n" +
                "</script> \n";
          this.Page.RegisterClientScriptBlock(RegistrationNameConfirmMessage,
                                                              sJSConfirmCode);
       }
    }
}

Future plans

More robustness.

History

  • Version 1.0, May 2005.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

micahbowerbank

Web Developer

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to create a customized confirmation alert in javascript Pingroupelizas3:53 25 Mar '10  
GeneralRelative paths to the images Pinmembernachoe4011:42 18 Dec '08  
Questionbinding EIB to an event handler PinmemberAniket Braganza12:22 17 Jan '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 9 May 2005
Article Copyright 2005 by micahbowerbank
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid