简体   繁体   中英

How to call javascript function from asp.net code behind function

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.

Use ClientScriptManager.RegisterClientScriptBlock

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

Regards,

据我所知您无法做到这一点。您可以在页面加载之前或在执行服务器端代码之后运行javascript代码。无法在javascript之间执行c#代码,然后返回c#。

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM