避免页面刷新上的按钮单击事件 [英] Avoid button click event on page refresh
本文介绍了避免页面刷新上的按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个网页,其中有一个按钮,并做了一些编码,当我刷新页面时,该按钮单击事件已触发,请让我知道如何避免在页面刷新时使用它.
谢谢你
Mohd Wasif
hi all ,
I have a web page in that i have a button and did some coding in that when i refresh page that button click event is firing please let me know how to avoid it on page refresh.
Thanking You
Mohd Wasif
推荐答案
这是最终的目的
http://www.codeproject.com/KB/aspnet/RefreshPage.aspx
OR
This is a final destinaion
http://www.codeproject.com/KB/aspnet/RefreshPage.aspx
OR
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // If page loads for first time
{
// Assign the Session["update"] with unique value
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
//=============== Page load code =========================
//============== End of Page load code ===================
}
}
protected void btnDisplay_Click(object sender, EventArgs e)
{
// If page not Refreshed
if (Session["update"].ToString() == ViewState["update"].ToString())
{
//=============== On click event code =========================
lblDisplayAddedName.Text = txtName.Text;
//=============== End of On click event code ==================
// After the event/ method, again update the session
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
else // If Page Refreshed
{
// Do nothing
}
}
protected override void OnPreRender(EventArgs e)
{
ViewState["update"] = Session["update"];
}
太好了……... !!
Great..........!
在您的Page_Load事件中添加以下代码,一切都很好.
Add the following code in your Page_Load event, and it will be all fine.
//browser refresh fires the last event (for ex button click) again
//the following code prevents this action.
if (!IsPostBack)
{
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"].ToString();
}
尝试理解代码.让我知道你是否不明白.
PS:我从网络上的某个地方(现在不记得链接了)获取了此解决方案,并在我的一个项目中使用了它.
干杯!
Ankur
Try and understand the code. Let me know if you don''t get it.
PS: I took this solution from somewhere on the web (I don''t remember the link now) and used it in one of my projects.
Cheers!
Ankur
如 ^ ],您可以考虑使用发布/重定向/获取 [ ^ ]模式可以避免这种情况
如果这不适用于您的页面,则可能需要查看使用Session \ ViewState来处理此问题
例如会议
http://csharpdotnetfreak.blogspot.com/2008/12/detect- browser-refresh-to-avoid-events.html [ ^ ]
ASP.Net中的刷新页面问题 [
As metioned in this thread[^] you might consider using a Post/Redirect/Get[^] pattern to avoid this
If this isn''t applicable to your page, you might want to look at using either Session \ ViewState to handle this
e.g. Session
http://csharpdotnetfreak.blogspot.com/2008/12/detect-browser-refresh-to-avoid-events.html[^]
Refresh Page Issue in ASP.Net[^]
这篇关于避免页面刷新上的按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文