Click here to Skip to main content
13,201,940 members (72,603 online)
Click here to Skip to main content
Add your own
alternative version

Stats

145.2K views
2.2K downloads
44 bookmarked
Posted 21 Mar 2003

ImageButton Control in ASP.NET

, 21 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Assigning disabled image to ImageButton Control

Sample Image - imageButtons.gif

Introduction

As you know, in the ASP.NET ImageButton can not be displayed as disabled. I have written the below code in order to solve this problem.

With this control you can easily show a disabled ImageButton. You can assign four different values for imageURL in this control.

  1. ImageURL
  2. DisableImageURL
  3. MouseOverImageURL
  4. MouseOutImageURL

The code

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ImageButtonTest
{
    /// <SUMMARY>
    /// Summary description for WebForm1.
    /// </SUMMARY>

    public class WebForm1 : System.Web.UI.Page
    {

        protected msWebControlsLibrary.ExImageButton btnSave;
        protected msWebControlsLibrary.ExImageButton btnCancel;
        protected msWebControlsLibrary.ExImageButton btnNew;
        protected msWebControlsLibrary.ExImageButton btnDelete;
        protected msWebControlsLibrary.ExImageButton btnSearch;
        protected System.Web.UI.HtmlControls.HtmlGenericControl divMain;
        protected System.Web.UI.WebControls.TextBox kontrol;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here

            if (!Page.IsPostBack)
            {
                changedefault();
            }
        }

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET 
            // Web Form Designer.
            //

            InitializeComponent();
            base.OnInit(e);
        }

        /// <SUMMARY>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </SUMMARY>

        private void InitializeComponent()
        {    
            this.btnSave.Click += new 
                    System.EventHandler(this.btnSave_Click);
            this.btnCancel.Click += new 
                    System.EventHandler(this.btnCancel_Click);
            this.btnNew.Click += new 
                    System.EventHandler(this.btnNew_Click);
            this.btnDelete.Click += new 
                    System.EventHandler(this.btnDelete_Click);
            this.btnSearch.Click += new 
                    System.EventHandler(this.btnSearch_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        #region ToolBarButton Events

        private void changeallfalse()
        {
            btnSave.Enabled      = false;
            btnDelete.Enabled = false;
            btnNew.Enabled = false;
            btnCancel.Enabled = false;
            btnSearch.Enabled = false;  
        }

        private void changeupdate()
        {
            if (kontrol.Text!="0" )
            {
                btnSave.Enabled = true;
                btnDelete.Enabled = true;
                btnNew.Enabled = true;
                btnCancel.Enabled = true;
                btnSearch.Enabled = true;

                kontrol.Text = "2";
            }

        }

        private void changenew()
        {

            btnSave.Enabled      = true;
            btnDelete.Enabled = false;
            btnNew.Enabled = false;
            btnCancel.Enabled = true;
            btnSearch.Enabled = false;

            kontrol.Text = "1";
        }
          
        private void changenavigate()
        {
            btnSave.Enabled      = false;
            btnDelete.Enabled = false;
            btnNew.Enabled = true;
            btnCancel.Enabled = true;
            btnSearch.Enabled = true;
        }

        private void changedefault()
        {
            btnSave.Enabled      = false;
            btnDelete.Enabled = false;
            btnNew.Enabled = true;
            btnCancel.Enabled = true;

            kontrol.Text = "0";
            btnSearch.Enabled = true;

        }

        #endregion

        #region ToolBarButton ClickEvents

        private void btnSave_Click(object sender, System.EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            changedefault();
        }

        private void btnNew_Click(object sender, System.EventArgs e)
        {
            changenew();
        }

        private void btnDelete_Click(object sender, System.EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            if (kontrol.Text=="2") { changedefault();return; }
            kontrol.Text = "0";

            btnCancel .Enabled = false;
            btnSave.Enabled      = false;

        }

        #endregion
    }
}

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

Share

About the Author

mikas
Web Developer
Turkey Turkey
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralNice article, thanks! Pin
Menthol3-Jul-08 21:45
memberMenthol3-Jul-08 21:45 
QuestionWhy a composite control? Pin
Robert Cheek3-Apr-06 12:23
memberRobert Cheek3-Apr-06 12:23 
GeneralA little re-touch Pin
Fernando Benavides17-Nov-04 7:08
sussFernando Benavides17-Nov-04 7:08 
GeneralRe: A little re-touch Pin
legcsabi26-May-08 12:10
memberlegcsabi26-May-08 12:10 
Here is my version:

public class ExImageButton : ImageButton
{
private string _strDisableImageURL;
private string _strEnableImageURL;
private string _strMouseOverImageURL;
private string _strMouseOutImageURL;

/// <summary>
/// ~ is supported!
/// </summary>
#region properties
[Bindable(true), Browsable(true), Category("Appearance") ]
public string DisableImageURL
{
get
{
return _strDisableImageURL;
}
set
{
_strDisableImageURL = value;
DisableImageURLFromViewState = _strDisableImageURL;
}
}

/// <summary>
/// Always provide aboslute path, ~ is not supported
/// </summary>
[Bindable(false), Browsable(true),Category("Appearance") ]
public string MouseOverImageURL
{
get
{
return _strMouseOverImageURL;
}
set
{
_strMouseOverImageURL = value;
MouseOverImageURLFromViewState = _strMouseOverImageURL;
}
}

/// <summary>
/// Always provide aboslute path, ~ is not supported
/// </summary>
[Bindable(false), Browsable(true),Category("Appearance") ]
public string MouseOutImageURL
{
get
{
return _strMouseOutImageURL;
}
set
{
_strMouseOutImageURL = value;
MouseOutImageURLFromViewState=_strMouseOutImageURL;
}
}

[Bindable(false), Category("Appearance"), DefaultValue(""),
Description("The URL of the image to be displayed for the button.")]
public override string ImageUrl
{
get
{
return _strEnableImageURL;
}
set
{
_strEnableImageURL = value;
EnableImageURLFromViewState = _strEnableImageURL;
}
}
#endregion

#region DesignPropertiesViewStates

protected string EnableImageURLFromViewState
{
get
{
return (string)ViewState["EnableImageURLFromViewState"];
}
set
{
ViewState["EnableImageURLFromViewState"] = value;
}
}

protected string DisableImageURLFromViewState
{
get
{
return (string) ViewState["DisableImageURLFromViewState"];
}
set
{
ViewState["DisableImageURLFromViewState"] = value;
}
}

protected string MouseOverImageURLFromViewState
{
get
{
return (string) ViewState["MouseOverImageURLFromViewState"];
}
set
{
ViewState["MouseOverImageURLFromViewState"] = value;
}
}

protected string MouseOutImageURLFromViewState
{
get
{
return (string) ViewState["MouseOutImageURLFromViewState"];
}
set
{
ViewState["MouseOutImageURLFromViewState"] = value;
}
}

override protected void OnPreRender(EventArgs ecx)
{
try
{
this.Attributes.Remove("OnMouseOver");
this.Attributes.Remove("OnMouseOut");
}
finally
{
if (!string.IsNullOrEmpty(MouseOverImageURLFromViewState))
this.Attributes.Add("OnMouseOver", "this.src='" + MouseOverImageURLFromViewState + "';");

if (!string.IsNullOrEmpty(MouseOutImageURLFromViewState))
this.Attributes.Add("OnMouseOut", "this.src='" + MouseOutImageURLFromViewState + "';");
}
if (Enabled)
{
string url = EnableImageURLFromViewState;
if (string.IsNullOrEmpty(url))
this.Visible = false;
else
{
this.ImageUrl = url;
this.Visible = true;
}
}
else
{
string url = DisableImageURLFromViewState;
if (string.IsNullOrEmpty(url))
this.Visible = false;
else
{
this.ImageUrl = url;
this.Visible = true;
}
}

base.OnPreRender(ecx);
}

#endregion

}
GeneralExImageButton and Netscape Pin
patur557-Jun-04 11:36
memberpatur557-Jun-04 11:36 
GeneralNeed help!! Pin
Anonymous1-Apr-03 10:44
sussAnonymous1-Apr-03 10:44 
GeneralRe: Need help!! Pin
Anonymous1-Apr-03 19:19
sussAnonymous1-Apr-03 19:19 
GeneralHi--..... Pin
Anonymous29-Mar-03 8:24
sussAnonymous29-Mar-03 8:24 
GeneralGreat button image Pin
benben24-Mar-03 1:23
memberbenben24-Mar-03 1:23 
GeneralRe: Great button image Pin
Anonymous24-Mar-03 5:47
sussAnonymous24-Mar-03 5:47 
GeneralNice introduction! Pin
Jörgen Sigvardsson22-Mar-03 5:52
memberJörgen Sigvardsson22-Mar-03 5:52 
GeneralRe: Nice introduction! Pin
Marc Clifton22-Mar-03 12:25
memberMarc Clifton22-Mar-03 12:25 
GeneralRe: Nice introduction! Pin
Jörgen Sigvardsson22-Mar-03 13:26
memberJörgen Sigvardsson22-Mar-03 13:26 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 22 Mar 2003
Article Copyright 2003 by mikas
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid