admin管理员组文章数量:1317741
I am currently working on asp 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:
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");
it shows message box successfully. I think the here may be is Response.Redirect()
has higher precedence pared to 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?
I am currently working on asp 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:
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");
it shows message box successfully. I think the here may be is Response.Redirect()
has higher precedence pared to 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?
- Does the Button1 placed in UpdatePanel? If so, use the ScriptManager.RegisterStartupScript method instead – IUnknown Commented Jun 13, 2011 at 11:20
-
1
Fyi, it's
type="text/javascript"
and notlanguage="..."
– ThiefMaster Commented Jun 13, 2011 at 11:21 - check ur backslashes. I think it might be like this: sb.Append("if (confirm('YES OR NO?')){ window.location.href = \"Default.aspx\"; }"); – Grace Commented Jun 13, 2011 at 12:07
5 Answers
Reset to default 6Do 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:
$('#Button1').click(function(){
if (confirm("Yes or No?")){
window.location = "/default.aspx";
}
});
There is an option of using adding javascript in client click event
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. After you register it, you immediately redirect to another page, and so the function is never executed.
add following code on Page_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.
as for ur Button1 click
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
I think it is better to add an attribute to the button at page load and leave the button_click function to just redirect. 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:
<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
本文标签: cJavascript getting ignored because of ResponseRedirect()Stack Overflow
版权声明:本文标题:c# - Javascript getting ignored because of Response.Redirect() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742015638a2413713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论