页面刷新 static变量问题

luckyTOTO 2004-11-01 11:05:50
我在一个页面的代码文件里定义了一个类的静态成员变量 然后放在page_load里每次加一,页面没刷新一次结构却加了2
public class XXX
{
static int num;
private void Page_Load()
{
num++;
显示num; 结果每次都显示加了2 刷新后显示1 3 5 7
}
............
}
放在构造函数里也是这个结构
是不是 页面没刷新一次,类要重新生成2次啊
...全文
238 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
再顶一下 马上揭帖
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
我在一个全新的页面上做了测试 结果和 skytear(铁匠) 说的一样
再问一下 我页面上有个datagrid 代码里有读取数据 排序 和分页代码 但是 应该没有运行啊
我在page_load里已经将绑定datagrid数据的代码注释了
怎么会刷新呢 ?????
baihecheng 2004-11-01
  • 打赏
  • 举报
回复
看上面的代码应该不会加2啊,你看看是不是别的地方有错
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
我已经把page_load函数改成这样了 还是1 3 5
void page_load()
{
num=num+1;
Label1.Text=num.ToString();
}
skytear(铁匠) 你是怎么测试的
继续关注。。。
  • 打赏
  • 举报
回复
还有其他地方的代码错误,上面的代码我帮你试过,是1,2,3,4,5。。。。。
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
static 变量应该自动会初始为0的吧 那么我每次刷新值都有累积 说明static变量可以在类之间传递,但是我就不明白 第一次是0 刷新后应该是1 怎么变成2了
继续关注。。。
冰河绝恋 2004-11-01
  • 打赏
  • 举报
回复
给个初始值跟踪一下。
forideal 2004-11-01
  • 打赏
  • 举报
回复
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label NumberLabel;

static int num;

private void Page_Load(object sender, System.EventArgs e)
{
num++;
NumberLabel.Text = num.ToString();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
困惑中
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
高手来看看
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
呵呵,先归档把,以后再次遇到在说。再次谢谢大家帮忙!!1
lhcoolhacker 2004-11-01
  • 打赏
  • 举报
回复
不明白,经测试值的确为1、2、3、4……
luckyTOTO 2004-11-01
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient ;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration ;

namespace TRS.chips
{
/// <summary>
/// ShopTroubleContents 的摘要说明。
/// </summary>
public class ShopMainWin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgShop;
protected System.Web.UI.HtmlControls.HtmlForm Form2;
protected System.Web.UI.WebControls.Button RetrunReport;
protected System.Web.UI.WebControls.Label Label4;

static string mySelect;
protected System.Web.UI.WebControls.Label Label1;
int num=0;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

num=num+1;
//Label1.Text=num.ToString();
Response.Write (num.ToString());
//return ;
/*if(!IsPostBack)
{

HttpCookie cookie = Request.Cookies ["MyFirstCookie"];
//判断是否已经登录
if ((null== cookie.Values["IsShop"])&&(null== cookie.Values["IsServer"]))
{
this.ShowAlertMsg("没有发现指定的cookie,请重新登录!");

}
//门店登录
if ( (cookie.Values["IsShop"]=="1")&&( cookie.Values["IsServer"]=="0"))
{
mySelect="select ReportCode,TroubleTitle,TroubleStateName,ReportDate,LiabilityPersonName,TroubleLiabilityPersonName,TroubleStateID from vw_TroubleReport_TroubleState where ShopCode='"+cookie.Values["ShopCode"]+"' and TroubleStateID BETWEEN 1 and 4 order by ReportDate desc " ;
dgShopLoginBind();
ShopGetBackColor("等待评价");
}
}*/
}

private void dgShopLoginBind() //门店人员登录时DataGrid中显示的信息
{

String dsn = ConfigurationSettings.AppSettings["TRS"];
SqlConnection myConnection = new SqlConnection(dsn);
myConnection.Open();
HttpCookie cookie = Request.Cookies ["MyFirstCookie"];
//获得故障上报的门店ID和未被归档的记录
//把记录写入DataSet ds中

SqlDataAdapter da=new SqlDataAdapter(mySelect,myConnection);
DataSet ds=new DataSet();
da.Fill(ds,"temp");
// ds.Tables["temp"].DefaultView.Sort=SortField;
// dgShop.DataSource=ds.Tables["temp"].DefaultView;
dgShop.DataSource=ds.Tables["temp"];
dgShop.DataBind();
da.Dispose();
myConnection.Close();
}
//如果上报表单的状态未4(等待评价),门店登陆时,此行记录显示未不同的颜色
private void ShopGetBackColor(string TroubleState)
{
// String dsn = ConfigurationSettings.AppSettings["TRS"];
// SqlConnection MyConn = new SqlConnection(dsn);
int i;

for(i=0;i<dgShop.Items.Count;i++)
{
if(dgShop.Items[i].Cells[5].Text==TroubleState)
{

dgShop.Items[i].BackColor=System.Drawing.Color.Red;

}
}

}
private void dgShop_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShop.CurrentPageIndex = e.NewPageIndex;
dgShopLoginBind();
ShopGetBackColor("等待评价");

}
public void ShowAlertMsg(string strMsg)
{

Page.RegisterStartupScript("key","<script language='javascript'>alert('" + strMsg + "');</script>");
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgShop.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShop_PageIndexChanged);
this.dgShop.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgShop_SortCommand);
this.dgShop.SelectedIndexChanged += new System.EventHandler(this.dgShop_SelectedIndexChanged);
this.RetrunReport.Click += new System.EventHandler(this.RetrunReport_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgShop_SelectedIndexChanged(object sender, System.EventArgs e)
{

}

private void RetrunReport_Click(object sender, System.EventArgs e)
{
Response.Redirect("TroubleReport.aspx");
}

private void dgShop_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
HttpCookie cookie = Request.Cookies ["MyFirstCookie"];
switch((string)e.SortExpression) //通过SortExpression来确定(DataView)dv的排序方式
{


case "ReportCode":
mySelect="select ReportCode,TroubleTitle,TroubleStateName,ReportDate,LiabilityPersonName,TroubleLiabilityPersonName,TroubleStateID from vw_TroubleReport_TroubleState where ShopCode='"+cookie.Values["ShopCode"]+"' and TroubleStateID BETWEEN 1 and 4 order by ReportCode desc " ;
dgShopLoginBind() ;
break;
case "TroubleTitle":
mySelect=...;
dgShopLoginBind() ;
break;
case "ReportDate":
mySelect=...;
dgShopLoginBind() ;
break;
case "LiabilityPersonName":
mySelect=... ;
dgShopLoginBind() ;
break;
case "TroubleLiabilityPersonName":
mySelect=... ;
dgShopLoginBind() ;
break;
case "TroubleStateName":
mySelect=...;
dgShopLoginBind() ;
break;
}


// SortField=e.SortExpression;
// dgShopLoginBind();

}

我得Page_Lode()却是执行了2次,但是我把里面的全都注释掉了 怎么会还是执行2次呢。继续关注

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧