日期:2014-05-18  浏览次数:20429 次

这网页为什么刷新也调用按钮函数?
第一个文件名:
test.aspx

<%@   Page   Inherits=CodeBehindExample   Src= "codebehindcode.aspx.cs "%>
<html>
        <head>
        <title> LiangDianUp   Co.   ASP.NET   Momery </title>
        <meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
        </head>
        <body>
<Form   runat=server>
        <asp:Button   ID= "btnClick "   runat= "server "   Text= "计算结果 "  

onclick= "btnClick_Click "   />

<br> <hr   width= "70% "> <br>
<asp:Label   id=lblState   runat=server   />
</form>
        </body>
        </html>
=========================================
第二个文件名:
codebehindcode.aspx.cs

using   System;
using   System.Web.UI;
using   System.Web.UI.WebControls;

public   class   CodeBehindExample   :   Page
{
public   Label   lblState;
public   Button   btnClick;
public   static   int   n=1;

public   void   btnClick_Click(Object   Source,   EventArgs   e)
{
n   =   n+1;
lblState.Text= "你点击了 "+n+ "次按钮。 ";
}
}

============================
我不明白按F5   刷新时也更新计数。


------解决方案--------------------
是的,如果你点过一次button,再刷新浏览器,浏览器会回传并执行刚刚的操作
这是浏览器的机制,如果要避免,需要在后台做一些判断
------解决方案--------------------
刷新是什么意思?
刷新就是就上次的request再次提交到server进行处理
上个request做了什么,你刷新还是执行的上次的operation
------解决方案--------------------
按钮提交之后redirect当前页然后加参数给个随机值在后面如:
a.aspx页面
void button_click()
{
//.......
response.redirect( "a.aspx?thd= ")+datetime.now.tostring();
}