This is a migrated thread and some comments may be shown as answers.

Another "ToolBar as a Grid CommandItem" Question

3 Answers 79 Views
ToolBar
This is a migrated thread and some comments may be shown as answers.
Stuart Hemming
Top achievements
Rank 2
Stuart Hemming asked on 27 May 2009, 01:53 PM
Same setup; a UserControl with a grid which has a ToolBar as the CommandItem.

I'm reacting to events on my calling page to enable/disable buttons on the ToolBar using code like this ...
          function NodeClicked(sender, args) { 
            var node = args.get_node(); 
 
            if (GridCommandItem != null) { 
              GridCommandItem.trackChanges(); 
              var btn = GridCommandItem.findItemByValue("doStuff"); 
              btn.set_enabled(true); 
              GridCommandItem.commitChanges(); 
            } 
            AjaxRequest("ShowDocuments;" + node.get_value()); 
          } 
Where "GridCommandItem" is a reference to the ToolBar.

Now, this works but only without the AjaxRequest call. The button is enabled and subsequent postbacks honour the enabled state of the control. However, my handler for the "ShowDocuments" AjaxRequest calls ReBind() on the Grid control and this resets the state of the ToolBar buttons.

Why? And what can I do about it?

--
Stuart

3 Answers, 1 is accepted

Sort by
0
Stuart Hemming
Top achievements
Rank 2
answered on 28 May 2009, 11:19 AM
OK, I've got a workaround for this, but I'd be grateful for any commants anyone might have, especially if there's a better way.

My server-side method "ShowDocuments" calls RadGrid1.ReBind(). What I've done is created a methos in the UserControl that looks like this ...
        public void DocumentGridRebind() 
        { 
            SaveCommandItemState(); 
            RadGrid1.Rebind(); 
            LoadCommandItemState(); 
        } 
Where the methods either side of the Rebind() look like this...
       void LoadCommandItemState() 
        { 
            RadToolBar tb = (RadToolBar)RadGrid1.MasterTableView.Controls[0].Controls[0].Controls[0].Controls[0].Controls[1]; 
            foreach (KeyValuePair<string, ToolbarState> kvp in _ToolbarState) 
            { 
                RadToolBarButton btn = (RadToolBarButton)tb.FindItemByValue(kvp.Key); 
                if (btn != null
                { 
                    btn.Enabled = kvp.Value.Enabled; 
                    btn.Checked = kvp.Value.Checked; 
                } 
            } 
        } 
        void SaveCommandItemState() 
        { 
            RadToolBar tb = (RadToolBar)RadGrid1.MasterTableView.Controls[0].Controls[0].Controls[0].Controls[0].Controls[1]; 
            foreach (RadToolBarItem item in tb.Items) 
            { 
                // We're expressly looking for RadToolBarButtons only here... 
                if (!(item is RadToolBarButton)) continue
                _ToolbarState[item.Value] = new ToolbarState(item.Enabled, ((RadToolBarButton)item).Checked); 
            } 
        } 
I can't say I'm happy with the code on lines 3 and 16, but this is the only way I could find to get at the RadToolBar in the CommandItem.

Finally, _ToolbarState is defined like this ...
        Dictionary<string, ToolbarState> _ToolbarState 
        { 
            get 
            { 
                object o = Session["__DocumentGridToolbarState"]; 
                if (o == null
                { 
                    o = new Dictionary<string, ToolbarState>(); 
                    Session["__DocumentGridToolbarState"] = o; 
                } 
                return (Dictionary<string, ToolbarState>)o; 
            } 
            set 
            { 
                Session["__DocumentGridToolbarState"] = value; 
            } 
        } 

As I say, anythoughts on a better way to do this would be appreciated.

--
Stuart



0
Accepted
Iana Tsolova
Telerik team
answered on 29 May 2009, 01:54 PM
Hello Stuart,

I was able to replicate the described issue. It is due to the fact that when grid is rebound its controls are being re-initialized. And changes for RadToolBar performed on the client are "cleared" thus, despite the trackChanges() and commitCHanges() calls.
So one resolution is what you have shared with us. And other is to disable the RadToolBar buttons on the server instead.

Best wishes,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Stuart Hemming
Top achievements
Rank 2
answered on 01 Jun 2009, 06:46 AM
Iana,

Thanks. That's what I thought it was.

Thanks again for the confirmation.

--
Stuart
Tags
ToolBar
Asked by
Stuart Hemming
Top achievements
Rank 2
Answers by
Stuart Hemming
Top achievements
Rank 2
Iana Tsolova
Telerik team
Share this question
or