你好,我有一个网站,当用户点击一个按钮和页面回发时,如果用户刷新页面或点击F5,则再次调用按钮方法.
任何人都知道一些方法来阻止页面刷新而不将页面重定向到同一页面?
if(page.isRefresh)之类的东西......或者如果存在任何javascript解决方案更好.
这看起来有效......但是当我刷新它不会回发但在文本框中显示之前的值
http://www.dotnetspider.com/resources/4040-IsPageRefresh-ASP-NET.aspx
private Boolean IsPageRefresh = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
TextBox1.Text = "Hi";
}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"];
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!IsPageRefresh) // check that page is not refreshed by browser.
{
TextBox2.Text = TextBox1.Text + "@";
} …Run Code Online (Sandbox Code Playgroud)