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

Select the RadMenuItem after a page postback

8 Answers 288 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Tony
Top achievements
Rank 1
Tony asked on 08 Dec 2008, 05:20 PM
Hi,

I am using RadMenu in one of my aspx pages. I have some javascript on the "OnClientItemClicked". which sets some JS varaibles . After a postback, I am loosing all values of the variables, which has to be set based on the currently selected radmenu. Is there way to trigger the "OnClientItemClicked" for the currently selected RadMenu?

Thanks,
Tony

8 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 09 Dec 2008, 08:32 AM
Hi Tony,
You can try calling the "click" client-side method of the menu item of interest. However if that item postbacks you will trigger that postback by clicking the item. I suggest you extract the implementation of your event handler in a separate method and call that method instead:

function onItemClicked(sender, args)
{
   myMethod(args.get_item());
}

function myMethod(menuItem)
{
}

You can use the RegisterStartupScript method of the ScriptManager class to invoke JavaScript from the server-side.

Regards,
Albert
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Doug
Top achievements
Rank 1
answered on 31 Aug 2009, 02:15 AM
Hello Albert,

I'm facing the same issue but I don't completely understand your recommended technique.  Can you elaborate a bit?

Many thanks,

Doug
0
Princy
Top achievements
Rank 2
answered on 31 Aug 2009, 09:48 AM
Hi Doug,

I guess you want to invoke the Itemclick event after postback in order to find out values based on selected menu item. If so, you could pass the selected item text to client side and find the item by using findItemByText client method, then perform your code to find the value.

C#:
 
protected void Page_Load(object sender, EventArgs e)  
{  
    if (RadMenu1.SelectedItem != null)  
    {  
        string name=  RadMenu1.SelectedItem.Text;  
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "key""<script type='text/javascript'>myMethod('" + name + "'); </script>"false);  
    }  

JavaScript:
 
<script type="text/javascript">  
function myMethod(itemText)  
{  
    alert(itemText);  
    // findItemBy text and invoke the click event using " click "      
}      
</script> 
Checkout the following documenattion which dwescribes how to get the RadMenu clicent object:
Client-Side Basics

Thanks,
Princy.
0
Doug
Top achievements
Rank 1
answered on 31 Aug 2009, 01:37 PM
Thank you, Princy.  On the Page_Load, server-side, the SelectedItem is always null.  I am doing a set_selected on the client-side during the OnClientItemClicking event, but it is lost as soon as the page postback.  I am considering using the client-side to set a cookie that perhaps could be retrieved during the server-side Page_Load, then using the HighlightPath.  But, does that seem like a logical and workable approach?  Or, I can simply keep everything on the client-side, creating the cookie on selecting, then set_selected during the OnClientPageLoad ( ? ) event.  Is there a control setting that might accomplish what I am seeking - to set the last clicked menu item to selected?

Cheers,

Doug
0
Saranya
Top achievements
Rank 1
answered on 02 Jul 2013, 09:32 AM
Don't want to resurrect such an old thread. But I'm facing this issue as well.
My page load where I register the JS:
protected void Page_Load(object sender, EventArgs e)
        {
 ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "key", "<script type='text/javascript'>highlightPath('" + "MenuItemText" + "'); </script>", false); 
  }

My JS:
// Function to highlight the
                // selected menu item.
                function highlightPath(text) {
                    var WPMenu = $find("<%= RadMenu1.ClientID%>");                         
                    if (WPMenu != null) {
                        var selectedItem = WPMenu.findItemByText(text);
                        if (selectedItem != null) {
                            selectedItem.set_cssClass(text);
                            //item.get_parent() is RadMenuItem, not RadMenu  
                            if (selectedItem.get_parent() != item.get_menu())
                                highlightPath(selectedItem.get_parent());
                        }
                    }                              
                }


But WPMenu (below) always seems to be null.
 
I have even tried registering the script on Page_LoadComplete. Still nothing. Any alternate methods to retain the selected item after postback ? Right now I'm getting text from the URL. But, I want to remove this dependency on the URL.

P.S my page load code is on a content page and the js is on the master page. The RadMenu is on the master page.

Thanks


0
Saranya
Top achievements
Rank 1
answered on 02 Jul 2013, 10:21 AM
I've found an alternate way to do this.

Register start up script on Page_Load of every page, that calls a javascript function and passes a hard-coded integer value (based on the page which you are calling from) . This function updates a global integer variable 'selectedIndex' .

Now use this selectedIndex in your OnClientMenuLoad event to highlight the menu item.

Regards
0
Cameron
Top achievements
Rank 1
answered on 27 Oct 2013, 10:52 PM
Hi Saranya

This sounds like a workable solution.

Would you be kind enough to post the working code? 

Thanks.
0
Princy
Top achievements
Rank 2
answered on 28 Oct 2013, 01:53 PM
Hi Cameron,

Please have a look into the following sample code I tried which works fine at my end.

ASPX: (MasterPage)
<telerik:RadMenu ID="RadMenu1" runat="server" Skin="Web20">
    <Items>
        <telerik:RadMenuItem Text="File">
        </telerik:RadMenuItem>
        <telerik:RadMenuItem Text="View">
        </telerik:RadMenuItem>
        <telerik:RadMenuItem Text="Data">
        </telerik:RadMenuItem>
    </Items>
</telerik:RadMenu>

JavaScript: (MasterPage)
<script type="text/javascript">
    function highlightPath(text) {
        var radmenu = $find("<%= RadMenu1.ClientID%>");
        if (radmenu != null) {
            var selectedItem = radmenu.findItemByText(text);
            if (selectedItem != null) {
                selectedItem.set_selected(true);
            }
        }
    }
</script>

C#: (Content Page)
protected void Page_Load(object sender, EventArgs e)
{
    string selectedIndex = "View";
    string script = "function f(){highlightPath('" + selectedIndex + "'); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
}

Thanks,
Princy.
Tags
Menu
Asked by
Tony
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Doug
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Saranya
Top achievements
Rank 1
Cameron
Top achievements
Rank 1
Share this question
or