Another "ToolBar as a Grid CommandItem" Question

4 posts, 1 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 27 May 2009 Link to this post

    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

  2. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 28 May 2009 Link to this post

    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...
    1        void LoadCommandItemState() 
    2         { 
    3             RadToolBar tb = (RadToolBar)RadGrid1.MasterTableView.Controls[0].Controls[0].Controls[0].Controls[0].Controls[1]; 
    4             foreach (KeyValuePair<string, ToolbarState> kvp in _ToolbarState) 
    5             { 
    6                 RadToolBarButton btn = (RadToolBarButton)tb.FindItemByValue(kvp.Key); 
    7                 if (btn != null
    8                 { 
    9                     btn.Enabled = kvp.Value.Enabled; 
    10                     btn.Checked = kvp.Value.Checked; 
    11                 } 
    12             } 
    13         } 
    14         void SaveCommandItemState() 
    15         { 
    16             RadToolBar tb = (RadToolBar)RadGrid1.MasterTableView.Controls[0].Controls[0].Controls[0].Controls[0].Controls[1]; 
    17             foreach (RadToolBarItem item in tb.Items) 
    18             { 
    19                 // We're expressly looking for RadToolBarButtons only here... 
    20                 if (!(item is RadToolBarButton)) continue
    21                 _ToolbarState[item.Value] = new ToolbarState(item.Enabled, ((RadToolBarButton)item).Checked); 
    22             } 
    23         } 
    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 ...
    1         Dictionary<string, ToolbarState> _ToolbarState 
    2         { 
    3             get 
    4             { 
    5                 object o = Session["__DocumentGridToolbarState"]; 
    6                 if (o == null
    7                 { 
    8                     o = new Dictionary<string, ToolbarState>(); 
    9                     Session["__DocumentGridToolbarState"] = o; 
    10                 } 
    11                 return (Dictionary<string, ToolbarState>)o; 
    12             } 
    13             set 
    14             { 
    15                 Session["__DocumentGridToolbarState"] = value; 
    16             } 
    17         } 

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

    --
    Stuart



  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 29 May 2009 Link to this post

    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.
  5. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 01 Jun 2009 Link to this post

    Iana,

    Thanks. That's what I thought it was.

    Thanks again for the confirmation.

    --
    Stuart
Back to Top