Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
315
WebExplorerBar issue setting selected item
posted

I am using the WebExplorerBar in a MasterPage for navigation and I need to maintain the state (both the expanded and selected items) of the WebExplorerBar between postbacks and page navigations.  I looked at this example:

http://es.infragistics.com/community/forums/t/47906.aspx

There were 2 issues with this example:

1) It didn't handle group child items having child items themselves

2) It did not save/restore the selected node properly (expanded was working as long as group child items did not have child items)

I wrote a class to save the state (expanded/selected), which I store in the session and use to keep the state of the WebExplorerBar.  I works fine for the Expanded property of all of the nodes, but the Selected issue from the example remains (the selected node in the WebExplorerBar sometimes works, sometimes it defaults to the first item, sometimes it selects the previously selected item.)  I have verified that my code for saving/setting the state works properly for Expanded and also works properly for Selected (it only sets one node to be Selected and it is the previously selected node).

It seems like something on the client side is changing the highlighted node after I have set the Selected property (expanded state of all node is restored correctly).  Can someone help me with this?

// Class to save state

public class WebExplorerBarItemState
    {
        public bool NodeState { get; set; }
        public bool Selected { get; set; }
        public List<WebExplorerBarItemState> ItemStates = new List<WebExplorerBarItemState>();
    }

    /// <summary>
    /// Class to save the state (expanded/selected) of all nodes in the WebExplorerBar
    /// control so we can restore it across pages.
    /// </summary>
    public class WebExplorerBarSate
    {
        public List<WebExplorerBarItemState> _groupStates = new List<WebExplorerBarItemState>();

        public WebExplorerBarSate(WebExplorerBar webExplorer)
        {
            foreach (ExplorerBarGroup group in webExplorer.Groups)
            {
                WebExplorerBarItemState groupState = new WebExplorerBarItemState();
                groupState.NodeState = group.Expanded;
                groupState.Selected = group.Selected;
                _groupStates.Add(groupState);

                if (group.HasChildren)
                    GetItemStates(group.Items, groupState);
            }
        }

        private void GetItemStates(ExplorerBarItemCollection items, WebExplorerBarItemState itemState)
        {
            foreach (ExplorerBarItem subItem in items)
            {
                WebExplorerBarItemState subItemState = new WebExplorerBarItemState();
                subItemState.NodeState = subItem.Expanded;
                subItemState.Selected = subItem.Selected;
                itemState.ItemStates.Add(subItemState);

                if (subItem.HasChildren)
                    GetItemStates(subItem.Items, subItemState);
            }
        }

        public static void SetWebExplorerBarState(WebExplorerBar webExplorer, WebExplorerBarSate webState)
        {
            foreach (ExplorerBarGroup group in webExplorer.Groups)
            {
                group.Expanded = webState._groupStates[group.Index].NodeState;
                group.Selected = webState._groupStates[group.Index].Selected;

                if (group.HasChildren)
                    SetItemStates(group.Items, webState._groupStates[group.Index]);
            }
        }

        private static void SetItemStates(ExplorerBarItemCollection items, WebExplorerBarItemState itemState)
        {
                foreach (ExplorerBarItem subItem in items)
                {
                    subItem.Expanded = itemState.ItemStates[subItem.Index].NodeState;
                    subItem.Selected = itemState.ItemStates[subItem.Index].Selected;

                    if (subItem.HasChildren)
                        SetItemStates(subItem.Items, itemState.ItemStates[subItem.Index]);
                }
        }
    }

        // Master page event handlers

        protected void webNavigation_ItemSelected(object sender, Infragistics.Web.UI.NavigationControls.ExplorerBarItemSelectedEventArgs e)
        {
            // Store the state (selected/expanded) of the nav menu
            Session["_WebExplorerState_Master_"] = new WebExplorerBarSate(webNavigation);

            string redirectURL = e.NewSelectedItem.Value.ToString();
            if (redirectURL != "")
            {
                Response.Redirect(redirectURL);
            }
        }

        protected void webNavigation_PreRender(object sender, EventArgs e)
        {
                // Restore the state (selected/expanded) of the nav menu
                WebExplorerBarSate state = (WebExplorerBarSate)(Session["_WebExplorerState_Master_"]);
                if (state != null)
                {
                    WebExplorerBarSate.SetWebExplorerBarState(webNavigation, state);
                }
        }

        // WebExplorerBar control in master page

<igb:WebExplorerBar ID="webNavigation" runat="server" GroupExpandBehavior="AnyExpandable"
                                                                OnPreRender="webNavigation_PreRender"
                                                                onitemselected="webNavigation_ItemSelected">
                                                                <Groups>                                                              
                                                                    <igb:ExplorerBarGroup Text="Home" ImageUrl="./images/icon_home_gray.png" Value="./main.aspx" Selected="true">
                                                                    </igb:ExplorerBarGroup>
                                                                    <igb:ExplorerBarGroup Text="Status" ImageUrl="./images/icon_status_gray.png">
                                                                        <Items>

...................................................

                                                                    </igb:ExplorerBarGroup>
                                                                </Groups>
                                                                <AutoPostBackFlags ItemClick="Off" ItemSelected="On" />
                                                            </igb:WebExplorerBar>