2

I am having a bit of a problem. I have the following code:

if (GetSelectedUsers().Count() > 0) {
    string script = "alert('not show this message');";
    System.Web.UI.ScriptManager.RegisterClientScriptBlock(btnActivateDeactivate, this.GetType(), "Test", script, true);
    Response.Redirect("FUserManagement.aspx");
} else {
    string script = "alert('This Message works');";
    System.Web.UI.ScriptManager.RegisterClientScriptBlock(btnActivateDeactivate, this.GetType(), "Test", script, true);
}

The second message works, but the first does not. I am new to ASP.NET. Can you help to archive this type of functionality? The problem in appears to be in the Response.Redirect method. When I comment it out, everything works normally, but I cant delete Response.Redirect as this is part of my functionality.

1
  • 1
    If you use Redirect then the alert is not displayed... so you have to use another logic, perhaps you could use Response.Redirect("FUserManagement.aspx?result=1"); Then in the page load of FUserManagement you could check the querystring and if you found the parameter "result" you can register the script for the alert. Commented May 4, 2011 at 10:54

2 Answers 2

3

That's because subsequent call to HttpResponse.Redirect stops the processing of the current request and sends 302 Moved temporarily with an empty response body.

If you want to display an alert box and redirect the user to another page after he closes the box then you could try doing the following:

Response.Clear();
Response.Write("<script>alert('You will be now redirected.'); location.href = 'FUserManagement.aspx';</script>");
Sign up to request clarification or add additional context in comments.

Comments

1

The 1st message will not display because you are writing the script to the page but then redirecting to a different page before it gets sent to the browser.

What are you trying to achieve? Display a JS alert and then redirect to another page?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.