如何从asp.net代码函数调用javascript函数

时间:2022-01-04 15:00:27

javascript code

javascript代码

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

aspx file

aspx文件

    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.

有没有办法做到这一点。我尝试过scriptmanager但没有成功。请帮忙 。谢谢。

3 个解决方案

#1


1  

Use ClientScriptManager.RegisterClientScriptBlock

使用ClientScriptManager.RegisterClientScriptBlock

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

请参阅以下链接。 http://msdn.microsoft.com/en-us/library/btf44dc9.aspx

Regards,

问候,

#2


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

据我所知你不能这样做。你可以在页面加载之前或服务器端代码执行之后运行javascript代码。你不能让javascript在c#代码执行之间工作,然后返回c#。

#3


0  

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

ASP.NET菜单控件无法在单击事件上注册javascript方法。

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来玩DOM来实现。

jQuery Code:

jQuery代码:

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

我希望这能解决你的问题......

#1


1  

Use ClientScriptManager.RegisterClientScriptBlock

使用ClientScriptManager.RegisterClientScriptBlock

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

请参阅以下链接。 http://msdn.microsoft.com/en-us/library/btf44dc9.aspx

Regards,

问候,

#2


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

据我所知你不能这样做。你可以在页面加载之前或服务器端代码执行之后运行javascript代码。你不能让javascript在c#代码执行之间工作,然后返回c#。

#3


0  

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

ASP.NET菜单控件无法在单击事件上注册javascript方法。

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来玩DOM来实现。

jQuery Code:

jQuery代码:

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

我希望这能解决你的问题......

相关文章