简体   繁体   English

Javascript 由于 Response.Redirect() 而被忽略

[英]Javascript getting ignored because of Response.Redirect()

I am currently working on asp.net using C# and I need to show a message box and confirm the input from user and redirect to another page, my code is something like this:我目前正在使用 C# 处理 asp.net,我需要显示一个消息框并确认用户的输入并重定向到另一个页面,我的代码是这样的:

protected void Button1_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script language='javascript'>");
    sb.Append("if (confirm('YES OR NO?')){ /*some javascript code*/ }");
    sb.Append("</script>");

    Page.RegisterStartupScript("FocusScript", sb.ToString());
   Response.Redirect("Default.aspx");

}

here the problem is directly getting redirected to next page without showing message box.这里的问题是直接被重定向到下一页而不显示消息框。 if i remove Response.Redirect("Default.aspx");如果我删除Response.Redirect("Default.aspx"); it shows message box successfully.它成功显示消息框。 I think the here may be is Response.Redirect() has higher precedence compared to javascript我认为这里可能是Response.Redirect()javascript相比具有更高的优先级

I tried using我尝试使用

sb.Append("if (confirm('YES OR NO?')){ window.location.href = \"Default.aspx"; }\");

instead of using Response.Redirect() but page didn't got redirected, what should I do to resolve this problem?而不是使用Response.Redirect()但页面没有被重定向,我应该怎么做才能解决这个问题?

Do you absolutely need to handle this on the server side?你绝对需要在服务器端处理这个吗? Indeed this is where quite a simple thing is getting confusing.事实上,这是一件很简单的事情变得令人困惑的地方。

If you could, for instance, handle it with Javascript / jQuery, you could do something simple like:例如,如果你可以用 Javascript / jQuery 来处理它,你可以做一些简单的事情,比如:

$('#Button1').click(function(){
   if (confirm("Yes or No?")){
      window.location = "/default.aspx";
   }
});

There is an option of using adding javascript in client click event有一个在客户端点击事件中使用添加 javascript 的选项

eg:-例如:-

<asp:button id="Button1" OnClientClick="javascript:return confirm('Are you sure?');" runat="server" onclick="Button1_Click" />

and then in the code side simple redirect the page.然后在代码端简单地重定向页面。

Confirm is a JavaScript function and it executes on client side. Confirm是 JavaScript function,它在客户端执行。 After you register it, you immediately redirect to another page, and so the function is never executed.注册后,您立即重定向到另一个页面,因此 function 永远不会执行。

add following code on Page_LoadPage_Load上添加以下代码

Button1.Attributes.Add("onclick", "if(confirm('do you want to redirect?')){}else{return false}");

now ur button click will first trigger java confirmation modal box.现在你的按钮点击将首先触发 java 确认模态框。

as for ur Button1 click至于你的 Button1 点击

protected void Button1_Click(object sender, EventArgs e)
{
   Response.Redirect("Default.aspx");
}

The idea is - your codebehind Button1_Click event will be triggered only after user confirms by clicking OK这个想法是 - 只有在用户通过单击确定确认后才会触发您的代码隐藏 Button1_Click 事件

I think it is better to add an attribute to the button at page load and leave the button_click function to just redirect.我认为最好在页面加载时向按钮添加一个属性,然后让 button_click function 只重定向。 Something like this (Page Load):像这样的东西(页面加载):

button.attribute.add("onclick","Javascript:confirmFunction();")

in button click just have this:在按钮点击中只有这个:

protected void Button1_Click(object sender, EventArgs e)
{       
   Response.Redirect("Default.aspx");    
}

finally in JS have this:最后在 JS 中有这个:

<script>
function confirmFunction()
{
    if(Confirm("Yes OR NO") 
       //Add your JS Code
       return true; //will pass onto server
    else
       //Add your JS Code
       return false; //will not go to server
}
</script>

HTH高温高压

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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