admin 管理员组

文章数量: 1086019

I have the following problem. I have one form to input customer data. After that, the form will give the user message to indicate that it has been done successfully:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
                                    "alert('Successfully input');", true);

Then, it will reload the page using the following:

Response.Redirect("customer_data.aspx");

The problem that I have is that the first mand does not work if I put the second one. If remove the second one, it works fine. I have tried even to use:

try
{
    ScriptManager.RegisterStartupScript(this, GetType(), "Success",                                         "alert('Successfully input');", true);
}
finally
{
    Response.Redirect("customer_data.aspx");
}

but again the first mand does not work. Please help.

I have the following problem. I have one form to input customer data. After that, the form will give the user message to indicate that it has been done successfully:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
                                    "alert('Successfully input');", true);

Then, it will reload the page using the following:

Response.Redirect("customer_data.aspx");

The problem that I have is that the first mand does not work if I put the second one. If remove the second one, it works fine. I have tried even to use:

try
{
    ScriptManager.RegisterStartupScript(this, GetType(), "Success",                                         "alert('Successfully input');", true);
}
finally
{
    Response.Redirect("customer_data.aspx");
}

but again the first mand does not work. Please help.

Share Improve this question edited Mar 10, 2014 at 21:17 Michael Petrotta 61k27 gold badges152 silver badges181 bronze badges asked Mar 10, 2014 at 19:57 user2103335user2103335 634 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

That's because ASP.NET shields you from understanding what happens on client

Response.Redirect("customer_data.aspx");

Sends out http header

Location=customer_data.aspx

And

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
                                "alert('Successfully input');", true);

Renders

<script type="text/javascript">
  alert('Successfully input');
</script>

at the end of page. Naturally if location is set in http header, browser performs redirect and scripts on page are not executed. You can perform the redirection in javascript after displaying the alert:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
  "alert('Successfully input');location.href='customer_data.aspx'", true);

I think the Response.Redirect to the same page is being detected as causing a loop. You can try reloading using script instead:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
    "alert('Successfully input');window.location.reload(true);", true);

You can use something like this:

string answer = "Success";    
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect",
            "alert('" + answer + "'); window.location.href='index.aspx';", true);

本文标签: cResponseRedirect eliminates the script manager actionStack Overflow