ASP.Net防止刷新自动触发事件的解决方案

来源:互联网 发布:百视通网络电视 编辑:程序博客网 时间:2024/06/13 23:28
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
使用ASP.Net,在刷新页面的时候会自动触发服务器端的事件。举个简单的例子,如:一个注册页面,我们填写完注册信息之后,如果按F5刷新之后,会自动触发到Button事件上,这样就造成了又注册了一次的麻烦。

我先前的解决方案:用关键字查询,如有相同,则提示已有此用户。

但是这种方案不能在没有表识的情况下使用,他会重新提交。鉴于此,我寻找了一种新的解决方案,很庆幸,在朋友的提点下,迸发了这样的解决方案,肯定有更好的解决方案,不吝赐教。

解决思路:刷新时,捕捉KeyPress,让他触发其他无用的事件。

1、捕捉F5事件的javascript
window.document.onkeydown = KeyStroke;

function KeyStroke()
{
var key = event.keyCode;

event.srcElement.releaseCapture();
if(key == 116)
{
document.getElementById("Button1").click();
event.keyCode=0;
event.returnValue=false;
}
}

2、ASPx页面放置一个Button

<ASP:Button id="Button1" style="Z-INDEX: 102; LEFT: 344px; POSITION: absolute; TOP: 408px; WIDTH: 0px;" runat="server"
Text="Button"></ASP:Button>

3、Button事件
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write( "You have pressed the key F5");
}

这个“舍车保帅”的方案,能解决刷新自动触发事件的解决方案这个小问题,如果谁有更好的方案,希望告诉我一声,不胜感激!

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击