Select the RadMenuItem after a page postback

9 posts, 0 answers
  1. Tony
    Tony avatar
    8 posts
    Member since:
    Jul 2008

    Posted 08 Dec 2008 Link to this post

    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
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 09 Dec 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Doug
    Doug avatar
    73 posts
    Member since:
    Mar 2008

    Posted 30 Aug 2009 Link to this post

    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
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 31 Aug 2009 Link to this post

    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.
  6. Doug
    Doug avatar
    73 posts
    Member since:
    Mar 2008

    Posted 31 Aug 2009 Link to this post

    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
  7. Saranya
    Saranya avatar
    10 posts
    Member since:
    Apr 2013

    Posted 02 Jul 2013 Link to this post

    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


  8. Saranya
    Saranya avatar
    10 posts
    Member since:
    Apr 2013

    Posted 02 Jul 2013 Link to this post

    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
  9. Cameron
    Cameron avatar
    1 posts
    Member since:
    Jun 2013

    Posted 27 Oct 2013 Link to this post

    Hi Saranya

    This sounds like a workable solution.

    Would you be kind enough to post the working code? 

    Thanks.
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2013 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017