2013-07-19 78 views
1

JavaScript代码如何调用从asp.net代码JavaScript函数后面功能

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

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. 

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

+0

的可能重复[参考:为什么PHP(或其他服务器端)在我的javascript代码不行?] (http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin

+0

你是什么样的菜单使用?也许你可以通过在客户端确认并在YES上提高服务器端事件来实现此目的。 – yogi

回答

0

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

0

这是不可能的ASP.NET菜单控制上注册click事件JavaScript方法。

我希望我明白你的问题是正确的!您需要在客户端(浏览器)上显示的其中一个菜单项目中的确认框没有回发。

这只能通过在文档呈现后使用jQuery使用jQuery来实现。

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; 
        } 
       }); 
      } 
     }); 

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

相关问题