Click here to Skip to main content
15,884,388 members
Articles / Web Development / HTML
Article

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

Rate me:
Please Sign up or sign in to vote.
3.38/5 (7 votes)
9 May 20051 min read 42.8K   22   3
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.

Image 1

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.

C#
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:

C#
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:

C#
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


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to create a customized confirmation alert in javascript Pin
elizas25-Mar-10 3:53
elizas25-Mar-10 3:53 
When you implement a delete feature in your application, a very basic additional feature which is always expected to be implemented is a confirmation box, so that the user gets a prompt before doing some delete activity.

For implementing this, there is already a Javascript function called confirm().
The syntax for this is very simple and i am sure every developer is aware of this. The syntax is as follow:

var confirm = confirm("Are you sure you want to do this?");
if(confirm)
{
//do your delete operation
}
else
{
return false;
}

http://www.mindfiresolutions.com/How-to-create-a-customized-confirmation-alert-in-javascript-631.php[^]
Cheers,
Eliza

GeneralRelative paths to the images Pin
nachoe4018-Dec-08 11:42
nachoe4018-Dec-08 11:42 
Questionbinding EIB to an event handler Pin
Aniket Braganza17-Jan-06 12:22
Aniket Braganza17-Jan-06 12:22 

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

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