1

javascript code

function logoutPop() {
       var a = confirm("are you sure !");
       if (a = true)
           return true;
       else return false;
    }

aspx file

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            string selectedItem = e.Item.Value;
            if (selectedItem == "logout")
            {
// here i want to call javascript function logoutPop() and check return value. if true want to redirect login page else nothing.

Is there any way to do this . I tried scriptmanager but not success . Please help . Thank you.

link2jagann
  • 183
  • 1
  • 5
  • 12
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin Jul 19 '13 at 11:18
  • What kind of menu are you using? May be you can achieve this by confirming on client side first & raising server side event on YES. – yogi Jul 19 '13 at 11:19

3 Answers3

1

Use ClientScriptManager.RegisterClientScriptBlock

See the following link. http://msdn.microsoft.com/en-us/library/btf44dc9.aspx

Regards,

Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
0

As far as i know you can't do that.You can run javascript code before page load or after the server side code has been executed.You can't make javascript work in between c# code execution and then return back to c#.

iJade
  • 23,144
  • 56
  • 154
  • 243
0

This is not possible with ASP.NET Menu Control to register a javascript method on click events.

I hope i understood your problem right! You want a Confirm box on one of the Menu Items which needs to be displayed on the Client Side(Browser) without a postback.

This can only be achieved by playing around with the DOM using jQuery after the document gets rendered.

jQuery Code:

$(document).ready(function () {
        $('#MainContent_mTest a').each(function () {
            var anchorHtml = $(this).html();
            if (anchorHtml == 'logout') {
                var clickEvent = this.onclick;
                this.onclick = "";
                $(this).bind('click', function (ev) {
                    if (confirm("Are you sure !")) {
                        clickEvent();
                    }
                    else {
                        return false;
                    }
                });
            }
        });

I hope this solves your problem...

Abhishek Jain
  • 2,597
  • 1
  • 18
  • 12