0

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);
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hitesh
  • 1,188
  • 5
  • 30
  • 52

2 Answers2

0

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.

Community
  • 1
  • 1
Paulie Waulie
  • 1,690
  • 13
  • 23
0

Change your script as follows

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

where i assumed Default.aspx is your page

iJade
  • 23,144
  • 56
  • 154
  • 243