我在一个页面的代码文件里定义了一个类的静态成员变量 然后放在page_load里每次加一,页面没刷新一次结构却加了2
public class XXX
{
  static int num;
  private void Page_Load()
  {
        num++;
        显示num;  结果每次都显示加了2 刷新后显示1 3 5 7
   }  
............
}
   放在构造函数里也是这个结构
是不是 页面没刷新一次,类要重新生成2次啊

解决方案 »

  1.   

    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
    }
      

  2.   

    static 变量应该自动会初始为0的吧 那么我每次刷新值都有累积 说明static变量可以在类之间传递,但是我就不明白 第一次是0 刷新后应该是1 怎么变成2了
    继续关注
      

  3.   

    我已经把page_load函数改成这样了 还是1 3 5
    void page_load()
    {
    num=num+1;
    Label1.Text=num.ToString();
    }
     skytear(铁匠)  你是怎么测试的
    继续关注
      

  4.   

    我在一个全新的页面上做了测试 结果和 skytear(铁匠)  说的一样
    再问一下 我页面上有个datagrid 代码里有读取数据 排序 和分页代码 但是 应该没有运行啊
    我在page_load里已经将绑定datagrid数据的代码注释了
    怎么会刷新呢 ?????
      

  5.   

    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次呢。继续关注