Polaris
Polaris

Reputation: 3793

Problem with JavaScript alert on button click

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.

Upvotes: 2

Views: 1143

Answers (2)

volpav
volpav

Reputation: 5128

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>");

Upvotes: 3

Fishcake
Fishcake

Reputation: 10764

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?

Upvotes: 1

Related Questions