2014-02-05 32 views
0

我有一个JS函数(显示警告框),并在ASP页面中有一个链接按钮,点击此链接按钮的代码后面的事件我想调用此函数,然后重定向到某个页面。从背后的代码调用JS函数问题

ASP代码

<script type="text/javascript" language="javascript"> 
    function myFunction() { 
     // executes when HTML-Document is loaded and DOM is ready 
     alert("document is ready!"); 
    }     
</script> 
<div> 
    <asp:LinkButton ID="lnkbtnChangePassword" runat="server" Text="Change Passwrod" OnClick="lnkbtnChangePassword_Click" ></asp:LinkButton> 
</div> 

C#代码

protected void lnkbtnChangePassword_Click(object sender, EventArgs e) 
{ 
    ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "myFunction()", true); 
    Response.Redirect("myPage.aspx"); 
} 

这里,当我点击链接按钮,它根本不显示提示框/窗口,并重定向到这一页。

回答

2

您应该使用LinkButton.ClientClick而不是LinkButton.Click事件。

Click事件在服务器上处理。当用户点击按钮时,页面被回发到服务器,代码在服务器端被执行。

ClientClick事件实际上是应该执行的JavaScript函数的名称。

你或许应该做这样的事情:

<asp:LinkButton ID="lnkbtnChangePassword" runat="server" Text="Change Passwrod" OnClientClick="showMyAlertAndSubmit()"></asp:LinkButton> 

<script type="text/javascript"> 
function showMyAlertAndSubmit(){ 
    alert("Your password is being changed"); 
    // submit your form 
} 
</script> 

为什么你当前的代码不起作用

您目前发送脚本显示的消息,并在同一重定向时间。这意味着浏览器会收到指示以显示消息并重定向,并在显示消息之前重定向用户。一种方法可能是从客户端重定向。例如:

protected void lnkbtnChangePassword_Click(object sender, EventArgs e) 
{ 
    ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "myFunction()", true); 
} 

,并在客户端

function myFunction() { 
    // show your message here 
    // do other things you need 
    // and then redirect here. Don't redirect from server side with Response.Redirect 
    window.location = "http://myserver/myPage.aspx"; 
} 

其他选项

,因为你需要保存数据和转向之前先进行服务器端的检查,可以使用Ajax调用服务器而不执行回发。 删除单击处理程序,并且只使用ClientClick:

function myFunction() { 
    $.ajax({ 
     type: "POST", 
     url: "myPage.aspx/MyCheckMethod", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 
      ShowAlert(); 
      window.location = "http://myserver/mypage.aspx"; 
     } 
    }); 
} 

有关详细信息please check this tutorial

+0

感谢您的答复,但以后可能我想,所以我必须在后面的代码使用显示该消息之前添加一些代码。 – AbdulAziz

+0

你能详细解释一下吗?你想点击,然后在服务器端做一些事情,然后回到客户端?在这种情况下,你需要一种不同的方法。例如,在提交和重定向之前将AJAX调用到服务器。 –

+0

是的,先生,这是我想要的,当我点击,然后在服务器端做一些事情,然后回到客户端。我应该编辑我的问题吗? – AbdulAziz

1

更改你的代码是这样的:

ASP代码

<script type="text/javascript" language="javascript"> 
    function myFunction() { 
    // executes when HTML-Document is loaded and DOM is ready 
    alert("document is ready!"); 
    }     
</script> 
<div> 
    <asp:LinkButton ID="lnkbtnChangePassword" runat="server" Text="Change Passwrod" OnClick="lnkbtnChangePassword_Click" OnClientClick="myFunction()" ></asp:LinkButton> 
</div> 

C#代码

protected void lnkbtnChangePassword_Click(object sender, EventArgs e) 
{ 
    Response.Redirect("myPage.aspx"); 
} 
1

改变你的JavaScript函数:

<script type="text/javascript" language="javascript"> 
    function myFunction() { 
     // alert will stop js execution 
     alert("document is ready!"); 
     // then reload your page 
     __doPostBack('pagename','parameter'); 
    }     
</script> 

而且从代码隐藏这一功能称之为:

protected void lnkbtnChangePassword_Click(object sender, EventArgs e) 
{ 
    //do your job here 
    //then call your js function 
    ScriptManager.RegisterStartupScript(this,this.GetType(), "CallMyFunction", "myFunction();", true); 
} 
+0

什么是'pagename'和'parameter'? – AbdulAziz

+0

http://www.codeproject.com/Articles/667531/__doPostBack功能 – homega52

0

什么你错过了等待的JavaScript警告对话框弹出,然后重定向到该页面。 为了做到这一点,你需要告诉按钮等待JavaScript警报对话框弹出,然后在服务器端工作。

ASPX:

<head runat="server"> 
    <title></title> 
    <script type="text/javascript"> 
     function alertBeforeSubmit() { 
      return alert('Document Ready'); 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Button Text="Do" runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" OnClientClick="return alertBeforeSubmit();" /> 
    </div> 
    </form> 
</body> 

CS:

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     Response.Redirect("Default.aspx"); 
    } 
相关问题