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

Problem with client event handlers on menu in UpdatePanel

1 Answer 136 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Scott R
Top achievements
Rank 1
Scott R asked on 26 Jun 2008, 06:19 AM
I have a RadMenu that exists in a MasterPage. The master page adds a client event handler to the OnClientItemClicking event. Here's the code:

    <script type="text/javascript">  
        function PageLoad()  
        {  
            var menu = $find("<%= MasterMenuInner.ClientID %>");  
            menu.add_itemClicking(MenuItemClicking);  
        }  
    </script>      
 

The PageLoad() function is called by the body "onload" event.

The content page also needs to handle the OnClientItemClicking event. Here's the code for that:

        function MenuItemClickingConfirmDelete(sender, args)  
        {    
             var itemValue = args.get_item().get_value();  
             if (itemValue == 'Delete')  
             {  
                if (confirm('Delete this invoice?'))  
                {  
                    args.set_cancel(true);  
                }  
             }  
        }  
 
        function InvoiceListPageLoad()  
        {  
            var menu = $find("<%= Master.MasterMenu.ClientID %>");  
            if (menu)  
            {  
                menu.add_itemClicking(MenuItemClickingConfirmDelete);  
            }  
            else  
            {  
                alert("<%= Master.MasterMenu.ClientID %> not found!");  
            }  
        }      
 

The "InvoiceListPageLoad()" function is also called by the body "onload" event.

So, everything works fine on the initial page load. Both event handlers fire as expected. Great.

The problem starts when the menu (on the master page) is updated. During an AJAX postback, I enable/disable some buttons. The menu is inside an UpdatePanel. The menu updates fine, but all client event handlers are "lost". I guess they are not part of the control state or view state. Obviously the body "onload" event doesn't fire after an AJAX postback.

I tried using the following code to re-register the event handlers after an AJAX postback:

ScriptManager.RegisterStartupScript(this, this.GetType(), "startup", "InvoiceListPageLoad();", true);

Unfortunately, I always get the alert box that says "ctl00_MasterMenuInner not found!"

How do I register a client event handler after an AJAX postback?

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 26 Jun 2008, 06:30 AM
Hi Scott R,

You could just set the OnClientItemClicking property of RadMenu to "MenuItemClicking". Your event handler will subscribe after ajax updates. The other option is to use the Sys.Application events instead of <body onload>.

Regards,
Albert
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Menu
Asked by
Scott R
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or