Disabling LinkButton, HyperLink and ImageButton completely

I was just going through one of my previous project and saw some code to disable the LinkButton, HyperLink and ImageButton; after going through it I remembered the issues we had faced with different browsers trying to disable these controls(with proper functionality, styling etc.). Hope posting these functions will help someone.

 

    /// <summary>
    /// Disables the link button.
    /// </summary>
    /// <param name="linkButton">The LinkButton to be disabled.</param>
    public static void DisableLinkButton(LinkButton linkButton)
    {
        linkButton.Attributes.Remove("href");
        linkButton.Attributes.CssStyle[HtmlTextWriterStyle.Color] = "gray";
        linkButton.Attributes.CssStyle[HtmlTextWriterStyle.Cursor] = "default";
        if (linkButton.Enabled != false)
        {
            linkButton.Enabled = false;
        }
 
        if (linkButton.OnClientClick != null)
        {
            linkButton.OnClientClick = null;
        }
    }
 
 
    /// <summary>
    /// Disables the hyper link.
    /// </summary>
    /// <param name="hyperLink">The HyperLink to be disabled.</param>
    public static void DisableHyperLink(HyperLink hyperLink)
    {
        hyperLink.NavigateUrl = string.Empty;
        hyperLink.Attributes.Remove("href");
        hyperLink.Attributes.CssStyle[HtmlTextWriterStyle.Color] = "gray";
        hyperLink.Attributes.CssStyle[HtmlTextWriterStyle.Cursor] = "default";
        if (hyperLink.Enabled != false)
        {
            hyperLink.Enabled = false;
        }
    }

 

    /// <summary>
    /// Disables the image button.
    /// </summary>
    /// <param name="imageButton">The ImageButton to be disabled.</param>
    public static void DisableImageButton(ImageButton imageButton)
    {
        imageButton.CommandArgument = string.Empty;
        imageButton.CommandName = string.Empty;
        imageButton.PostBackUrl = string.Empty;
        imageButton.Attributes.Remove("href");
        imageButton.Attributes.CssStyle[HtmlTextWriterStyle.Color] = "gray";
        imageButton.Attributes.CssStyle[HtmlTextWriterStyle.Cursor] = "default";
        if (imageButton.Enabled != false)
        {
            imageButton.Enabled = false;
        }
 
        if (imageButton.OnClientClick != null)
        {
            imageButton.OnClientClick = null;
        }
    }

10 Comments

Add a Comment

As it will appear on the website

Not displayed

Your website