简体   繁体   English

按钮单击事件的警报消息

[英]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. 我的页面aspx和按钮单击事件上都有一个按钮和文本框,我正在显示警报消息。

Now when I Run website and click on button it display message but after that when I press F5( Refresh) it again display message. 现在,当我运行网站并单击按钮时,它将显示消息,但之后,当我按F5(刷新)时,它将再次显示消息。 So My question how to remove message when I click F5. 所以我的问题是当我单击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 谢谢,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. 当您按F5刷新时,浏览器会将相同的请求再次发布到服务器,因此按钮处理程序再次被激发。

I advise you read a little about how Post And Gets work as well as how postbacks work in ASP. 我建议您阅读一些有关Post And Gets如何工作以及ASP中回发如何工作的信息。

Change your script as follows 如下更改脚本

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

where i assumed Default.aspx is your page 我认为Default.aspx是您的页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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