一个很基础的问题,但是很奇怪!大家来帮忙看看!!
我建立一个简单的asp.net项目(myTest)
在页面index.aspx中加入一个label,两个button.
定义了一个静态变量currentRow,主要来跟踪currentRow的变化!
我同时打开两个页面,分别进行操作!
假设在页面一里,当前值是3,页面二里当前值是1,我在页面二里点击了一下"下一条"
按钮,结果就变成了4,两个页面会相互影响,请问这是为什么?
以下是源程序:
namespace myTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button buttonNext;
protected System.Web.UI.WebControls.Button buttonPreview;
protected System.Web.UI.WebControls.Label labelLocation;
protected static int currentRow;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack )
{
currentRow=-1;
DisplayLocation();
}
}
#region Web Form Designer generated code
//显示当前位置
protected void DisplayLocation()
{
string i = Convert.ToString (currentRow+1);
labelLocation.Text = "当前位置:"+i+"/10";
}
//显示上一条
private void buttonPreview_Click(object sender, System.EventArgs e)
{
if(currentRow>0)
{
currentRow--;
}
DisplayLocation();
}
//显示下一条
private void buttonNext_Click(object sender, System.EventArgs e)
{
if(currentRow<9)
{
currentRow++;
}
DisplayLocation();
}
}
}