简体   繁体   中英

Alert message on button click event

I have a button and textbox on my page aspx and on button click event I am displaying alert message.

Now when I Run website and click on button it display message but after that when I press F5( Refresh) it again display message. So My question how to remove message when I click F5.

Below is My code for Button click:

  protected void btnExport_Click(object sender, EventArgs e)
  {
      DisplayMessage("There is no data to Export.", this);

  }

and

public void DisplayMessage(String strMessage, Control name)
{
    string script = "<script language='javascript'>alert('" + strMessage + "');</script>";
    ScriptManager.RegisterStartupScript(name, name.GetType(), "JSCR", script, false);
}

Thanks, Hitesh

You are instigating a client side event from server side which usually does not make sense.

The behaviour you are noticing is correct, when you clicked that button a request is posted to the server which calls your button click handler which then registers the client side script when the response is returned. When you press F5 to refresh your browsers is posting that exact same request again to the server, so the button handler is fired again.

I advise you read a little about how Post And Gets work as well as how postbacks work in ASP.

Change your script as follows

string script = "alert('hello');window.location.href='Default.aspx'";

where i assumed Default.aspx is your page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM