开发者

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, thi开发者_JS百科s.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.


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜