Enable / Disable Buttons

Oct 13, 2010 at 1:33 AM

Hi!

I'd like to know what is the best method to enable or disable buttons for any given toolbar.

Thanks!

(Sorry for asking so many doubts, I search for any documentation before posting here, I swear!! :)

Coordinator
Oct 13, 2010 at 5:34 PM

Hello James, I apologize for the lacking documentation. It's still in the writing unfortunately ( I didn't have time to finish it as I have other pressing matters in the coming release due this saturday). Asking here is the best way to get by, so you do well. 

A ToolBarItem can be disabled in several ways. You can do it declaratively (or at designtime through the toolbars property and drilling down to a toolbarset and subsequently to a ToolBarItem) by changing the Enabled property of the ToolBarItem. While it's easy, its not very convenient because any small change you make at designtime will serialize all the default properties in your page declaratively.

Another way is to do it through the skinning framework, but I've never gotten to documenting this either.

The easiest I can recommend is to do it in code. Basically, the toolbar items are created and populated during page_load stage, so you should try at a later phase such as Page_LoadComplete. In this handler just look for the ToolBarItem your interested in by drilling down in code eg : 

protected void Page_LoadComplete(object sender, EventArgs e)
    {
        /*A ToolBarSet holds a collection of ToolBarItem instances so :
         * ---------------------------------------
         First retrieve the ToolBarSet that you are interested in ; 
         ToolBarSets are indexed from 0 to x in the same order they appear in the editor. So if you wanted to retrieve the Bold item in the ToolBar,
         * which is contained in the first ToolBarSet, you can access it like this :
         */
        
        ToolBarSet tbs = Editor1.ToolBars[0];
        foreach (ToolBarItem tbi in tbs.Items)
        {
            if (tbi.Command.Equals("Bold", StringComparison.OrdinalIgnoreCase))
            {
                tbi.Enabled = false;
                break;
            }
        }
    }

The above code will disable the Bold button in the ToolBar.  Hopefully it's clear enough to get you started. 

Alessandro

Coordinator
Oct 24, 2010 at 8:26 PM
Edited Oct 24, 2010 at 8:27 PM

Hello, I just want to let you know that in the latest release I've rewritten the toolbars api on the client, and in the process I couldn't help but refactor "Command" to "Name".

This means when you upgrade and I encourage you to do so because this release has quite a bit of fixes, you'll need to refactor the code above to :

 

protected void Page_LoadComplete(object sender, EventArgs e)
    {
        /*A ToolBarSet holds a collection of ToolBarItem instances so :
         * ---------------------------------------
         First retrieve the ToolBarSet that you are interested in ; 
         ToolBarSets are indexed from 0 to x in the same order they appear in the editor. So if you wanted to retrieve the Bold item in the ToolBar,
         * which is contained in the first ToolBarSet, you can access it like this :
         */
        
        ToolBarSet tbs = Editor1.ToolBars[0];
        foreach (ToolBarItem tbi in tbs.Items)
        {
            if (tbi.Name.Equals("Bold", StringComparison.OrdinalIgnoreCase))
            {
                tbi.Enabled = false;
                break;
            }
        }
    }

 

Note the little change "Command" is now "Name".

Alessandro

Oct 31, 2014 at 3:54 AM
You can create a button and then customize it property by setting "Enabled" to either true or false.