I am writing asp.net project in C#.
I have a button in a page default.aspx, and I need javascript alert when I click the button and then update page.
I do this by the following way:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script language='javascript'>alert('OK');</script>");
Response.Redirect("default.aspx");
}
But javascript alert doesn’t occur.
So, how to make first appear javascript alert and then update page?
It is not working because you are calling the
Response.Redirect. Anything that happens before this on the current page will be ineffective, because the new page will immediately redirect before the current page is rendered.You have a couple of options, but the one I think you want is this…
The other option is to store the message you want to display in something like a session variable, and then show it on the new page after the redirection – but that is more complicated and requires the updating of the new page as well.