views:

149

answers:

1

Is there a method to globally disable the tooltips in a .net project Visual studio 2008. We have implemented tooltip in the web apps, but would like to turn it off for testing.

A: 

You can use a ControlAdapter for that:

namespace Your.Namespace
{
    public class ToolTipsDisablerAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
    {
        public static bool ToolTipsDisabled { get; set; }

        protected override void Render(HtmlTextWriter writer)
        {
            if (ToolTipsDisabled && Control.ToolTip != string.Empty)
            {
                Control.ToolTip = string.Empty;
            }

            base.Render(writer);
        }
    }
}

The Render method of the adapter gets called in place of the one of the controls it is linked to. The call to base.Render ensures the Render method of the control is called, after the adapter has modified the tooltip.

The adapter has to be registered in a browsers definition file to work:

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.WebControl" adapterType="Your.Namespace.ToolTipsDisablerAdapter" />
    </controlAdapters>
  </browser>
</browsers>

The browsers definition file has to be placed in the App_Browsers directory of the website, with a .browser extension.

With that done, you can enable/disable the tooltips globally by setting

ToolTipsDisablerAdapter.ToolTipsDisabled = testMode;

Edit: Placed the code in the Render method rather than PreRender. This avoids persisting the change across postbacks (and clogging the viewstate with useless tooltip values).

Mathieu Garstecki