为什么页面加载时不执行binddata显示数据?要点击页面的botton才能显示datagrid的数据??
跟private  protected 和Bind(intCurrentPageIndex);有关吗?using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class user_shiyan2 : System.Web.UI.Page
 {
    private static int intRecordCount;                      //記錄總數
    private static int intPageCount;                        //總頁數
    private static int intCurrentPageIndex;                    //當前頁碼
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton LinkButton3;
protected System.Web.UI.WebControls.LinkButton LinkButton4;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;                 
private DataView dv;  private void Page_Load(object sender, EventArgs e)
   {
   if (!Page.IsPostBack)
   {
   //將當前頁面設置為1,即顯示第一頁數據
   intCurrentPageIndex = 1;    //設置屬性
   // DataGrid1.AllowPaging = "True";
   DataGrid1.AllowPaging = true;
   // DataGrid1AllowCustomPaging = "True";
   DataGrid1.AllowCustomPaging = true;    //綁定數據(Bind()是一個自定義方法
   Bind(intCurrentPageIndex);
   }                   }public void Bind(int intCurrentPageIndex)
{
     string strConn = ConfigurationSettings.AppSettings["cnn"].ToString();
     //每次只取出一頁的數據
     string strSql = "select top 20 * from I_ToolLoc where TLTOOL not in (select top " + ( intCurrentPageIndex - 1)*20 + " TLTOOL from I_ToolLoc)";  if (TextBox1.Text != null && TextBox1.Text != "")
     {        strSql = "select top 20 * from I_ToolLoc where TLTOOL not in (select top " + (intCurrentPageIndex - 1) * 20 + " TLTOOL from I_ToolLoc where [TLCDTE] like '%" + TextBox1.Text + "%') and [TLCDTE] like '%" + TextBox1.Text + "%'";     }
      //得到總記錄數的SQL語句
      string strSqlCount = "select count(*) from I_ToolLoc ";
     if(TextBox1.Text != null && TextBox1.Text != "")
    {
         strSqlCount = "select count(*) from I_ToolLoc where    [TLCDTE] like '%" + TextBox1.Text + "%'";
     }
      //建立與數據庫的連接
      SqlConnection dbConnection = new SqlConnection(strConn);
      dbConnection.Open();
      //創建適配器的連接,并填充數據
       SqlDataAdapter dsAdapter = new SqlDataAdapter(strSql,dbConnection);
      DataSet ds = new DataSet();
      dsAdapter.Fill(ds);
      dv = ds.Tables[0].DefaultView;      //取得總記錄數      SqlCommand cmd = new SqlCommand(strSqlCount,dbConnection);
      intRecordCount = (Int32) cmd.ExecuteScalar();
     
      //關閉連接
      dbConnection.Close();
        
     //計算總頁數
     double dblRecordCount;
     dblRecordCount = System.Convert.ToDouble(intRecordCount);
     intPageCount = (Int32)Math.Ceiling(dblRecordCount / 20);     Label1.Text = intCurrentPageIndex.ToString();
     Label2.Text = intPageCount.ToString();
     //綁定數據
     DataGrid1.DataSource = dv;
     DataGrid1.DataBind();
  }     protected void LinkButton1_Click(object sender, EventArgs e)
     {
         intCurrentPageIndex = 1;
         //重新綁定數據
         this.Bind(intCurrentPageIndex);  
     }
     protected void LinkButton2_Click(object sender, EventArgs e)
     {
         intCurrentPageIndex = intCurrentPageIndex - 1;
         if (intCurrentPageIndex <= 0)
         {
             intCurrentPageIndex = 1;
         }
         //重新綁定數據
         this.Bind(intCurrentPageIndex);  
     }
     protected void LinkButton3_Click(object sender, EventArgs e)
     {
         intCurrentPageIndex = intCurrentPageIndex + 1;
          if(intCurrentPageIndex > intPageCount)
          {
              intCurrentPageIndex = intPageCount;
          }
          //重新綁定數據
           this.Bind(intCurrentPageIndex);  
     }
     protected void LinkButton4_Click(object sender, EventArgs e)
     {
         intCurrentPageIndex = intPageCount;
         //重新綁定數據
         this.Bind(intCurrentPageIndex);  
     }
private void InitializeComponent()
{


this.Load += new System.EventHandler(this.Page_Load); }     protected void Button1_Click(object sender, EventArgs e)
     {         intCurrentPageIndex = 1;
         //重新綁定數據
         this.Bind(intCurrentPageIndex);  
     }
}

解决方案 »

  1.   

    在BS结构下不要用private static int intRecordCount;                      //記錄總數
    private static int intPageCount;                        //總頁數
    private static int intCurrentPageIndex; 这样会造成线程操作混乱,用VIEWSTATE来代替STATIC
      

  2.   


    public void Bind(int intCurrentPageIndex)里面的intCurrentPageIndex是否与外部的 private static int intCurrentPageIndex存在命名混乱?
     
      

  3.   

     前台AutoEventWireup="true"就可以执行了pageload事件。
         AutoEventWireup="false"就不可以执行了pageload事件。但是我看见其他页面 AutoEventWireup="false"也照样能执行pageload事件这是为什么呢?为什么不能用private static int intRecordCount;  ?
      

  4.   


    你可以到网上查一下,STATIC在BS里面到底是起什么作用的,一旦声明STATIC,就表示该类的所有实例都可以使用这个变量,static是属于类本身的,并不属于类的实例!
      

  5.   

    我的意思是会不会intCurrentPageIndex的命名有问题,你把public void Bind(int intCurrentPageIndex)里面的参数名intCurrentPageIndex用别的名字试试看。
      

  6.   

    我不知道是否是这个原因,但是是我肯定不会这么命名参数,我只是提醒你下面红色的intCurrentPageIndex到底是引用函数Bind的参数int intCurrentPageIndex
    还是引用最上面你声明的private static int intCurrentPageIndex;public void Bind(int intCurrentPageIndex)
    {
         string strConn = ConfigurationSettings.AppSettings["cnn"].ToString();
         //每次只取出一頁的數據
         string strSql = "select top 20 * from I_ToolLoc where TLTOOL not in (select top " + ( intCurrentPageIndex - 1)*20 + " TLTOOL from I_ToolLoc)";  if (TextBox1.Text != null && TextBox1.Text != "")
    ...
      

  7.   

    在Asp.net中,你可以在页面的声明语句中设置AutoEventWireup属性,<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>哪么AutoEventWireup的具体含义是什么呢?当你将这个属性设置为true时,Asp.Net将页面的生命周期事件与一些特殊的方法进行关联,比如将你的Page_Load方法直接关联到页面生命周期的OnPageLoad事件。如果你将这个属性设置为false,那么这种自动的关联将不存在,你需要自己设置OnPageLoad事件的处理程序。一般你将这个属性设置为false后,你可以获取少许的性能提升。
      

  8.   

    这样设置还是不能自动加载数据,要点击button才能加载数据
    AutoEventWireup="false"private void InitializeComponent()
    {


    this.Load += new System.EventHandler(this.Page_Load);}只有AutoEventWireup="true"才能自动加载数据,