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

Datasource to SiteMapDataSource and Retaining the RadPanelBar state after URL navigation

3 Answers 57 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Hernan
Top achievements
Rank 1
Hernan asked on 03 May 2016, 04:36 PM

Hi I need retain the Satete of the panelbar after navigation, but also I need use databind this to SiteMapDataSource

 

This is the code that I use, but not Working, (the idea is expand the parent and set to selected the item node that match with the url (Or cointains this))

what is bad in the code?

<telerik:RadPanelBar RenderMode="Lightweight" OnItemDataBound="RadPanelBar1_ItemDataBound"  
                ID="RadPanelBarMenu" ViewStateMode="Enabled" EnableViewState="true" runat="server"  Width="100%" ExpandMode="SingleExpandedItem"
                DataSourceID="SiteMapDataSource1" DataFieldID="id" DataFieldParentID="ParentId" DataTextField="">
                <DataBindings>
                    <telerik:RadPanelItemBinding  Depth="0" Expanded="true" />
                </DataBindings>
            </telerik:RadPanelBar>

            <asp:SiteMapDataSource runat="server"  ID="SiteMapDataSource1" ShowStartingNode="false"></asp:SiteMapDataSource>

 

In code Behind

 

protected void RadPanelBar1_ItemDataBound(object sender, RadPanelBarEventArgs e)
    {
        System.Web.SiteMapNode row = (System.Web.SiteMapNode)e.Item.DataItem;
     
        string url_menu = row.Url.Replace(".aspx", "").ToLower();


        if (!string.IsNullOrEmpty(row.Url) && Request.Url.AbsoluteUri.ToLower().Contains(url_menu))
        {
            e.Item.Selected = true;

            e.Item.ExpandParentItems();

        }


    }

 

The sitemap

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode title="All">
<siteMapNode title="APP">
<siteMapNode title="Historical" url="~/webpages/HistoricalAppMobileSender.aspx" />
<siteMapNode title="Message" url="~/webpages/AppMobileSender.aspx" />
<siteMapNode title="Agenda" url="~/webpages/Scheduler.aspx" />
</siteMapNode>

</siteMap>

 

 

 

3 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 06 May 2016, 01:31 PM
Hello Hernan,

If I understand you correctly, what you are trying to achieve is once you navigate to a page by selecting a PanelBar item and you press the browser's back button to have the clicked item selected. This can be achieved through using a HiddenField control, which will hold the selected item's text and when the page loads again you can find the item with the saved text and select it using the PanelBar client-side methods. Here's how you can do that:
  • add a HiddenField control to your page
  • subscribe to the OnClientItemClicked event
  • in the event handler get the clicked item's text and save it in the HiddenField
  • in the pageLoad handler retrieve the saved value, use it to find the item and select it

<telerik:RadPanelBar RenderMode="Lightweight" OnItemDataBound="RadPanelBar1_ItemDataBound"
    ID="RadPanelBarMenu" ViewStateMode="Enabled" EnableViewState="true" runat="server" Width="100%" ExpandMode="SingleExpandedItem"
    DataSourceID="SiteMapDataSource1" DataFieldID="id" DataFieldParentID="ParentId" DataTextField="" OnClientItemClicked="OnClientItemClicked">
    <DataBindings>
        <telerik:RadPanelItemBinding Depth="0" Expanded="true" />
    </DataBindings>
</telerik:RadPanelBar>
 
<asp:SiteMapDataSource runat="server" ID="SiteMapDataSource1" ShowStartingNode="false"></asp:SiteMapDataSource>
 
<asp:HiddenField runat="server" ID="HiddenField1" />
 
<script>
    function pageLoad() {
        if ($telerik.$("#HiddenField1").val() != "") {
             
            var panelBar = $find("<%= RadPanelBarMenu.ClientID %>");
            var item = panelBar.findItemByText($telerik.$("#HiddenField1").val());
            $telerik.$("#HiddenField1").val("");
            item.select();
        }
    }
 
    function OnClientItemClicked(sender, args) {
        var item = args.get_item();
        if (item.get_navigateUrl() != "") {
            var text = args.get_item().get_text();
            $telerik.$("#HiddenField1").val(text);
        }
    }
</script>


Regards,
Ivan Danchev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Hernan
Top achievements
Rank 1
answered on 06 May 2016, 03:38 PM

Hello

Sorry the bad english

 

I am trying of get the selected item of PanelBar, but the problem is when this is have set the datasourceid whit the sitemap

If you see In the sitemap is the url for navigate to teh page

My idea is set this selected item in the ItemDataBound event using the url that the user is viewing for compare whit each item

review my code above.

For some reason the state is not maintained when I set the panelbar's properties in the refer event.

if (!string.IsNullOrEmpty(row.Url) && Request.Url.AbsoluteUri.ToLower().Contains(url_menu))
        {
            e.Item.Selected = true;

            e.Item.ExpandParentItems();

        }

 

Best regards

 

 

 

 

 

0
Veselin Tsvetanov
Telerik team
answered on 11 May 2016, 07:19 AM
Hello Herman,

Attached you will find a simple implementation of your scenario. At our end the state of the RadPanelBar is successfully saved and loaded in each of the pages.

Keep in mind that when binding to SiteMapDataSource, there is no need to set any properties to map fields from the data source to properties of the panel items. This means you could freely remove the DataFieldID, 
DataFieldParentID and the DataTextField properties from your RadPanelBar declaration as they are not used.

Regards,
Veselin Tsvetanov
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
PanelBar
Asked by
Hernan
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Hernan
Top achievements
Rank 1
Veselin Tsvetanov
Telerik team
Share this question
or