为什么页面加载时不执行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);
}
}
解决方案 »
- textarea数据库交互换行问题!高手指导
- 关于XP SP3安装IIS和microsoft sql server 2005的问题,希望大家仁者见仁,智者见智!
- 小测试,看看谁的程序最优,将汉字转换成汉语拼音。
- 执行到document.write("abc");这一句时怎么提示“没有可用于当前位置的源代码”
- 难道这里没有人知道如何实现无限级别分类吗
- 高手们,教教小弟吧!!!!!!!!!!
- 求救!简单问题!!!!!!
- 第三次问,怎样实现datagrid的循环显示?请帮我一下,先谢谢了!!
- 再次发表DATAGRID问题·关于批量得~~
- datagrid中通过确定对话框来删除记录,点击确定后怎样让删除操作进行?
- 请问如何对母版页编程,比如在用户未登录时显示”你好游客“,登录后显示你好XXX
- ASP.NET中网页倒计时
private static int intPageCount; //總頁數
private static int intCurrentPageIndex; 这样会造成线程操作混乱,用VIEWSTATE来代替STATIC
public void Bind(int intCurrentPageIndex)里面的intCurrentPageIndex是否与外部的 private static int intCurrentPageIndex存在命名混乱?
AutoEventWireup="false"就不可以执行了pageload事件。但是我看见其他页面 AutoEventWireup="false"也照样能执行pageload事件这是为什么呢?为什么不能用private static int intRecordCount; ?
你可以到网上查一下,STATIC在BS里面到底是起什么作用的,一旦声明STATIC,就表示该类的所有实例都可以使用这个变量,static是属于类本身的,并不属于类的实例!
还是引用最上面你声明的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 != "")
...
AutoEventWireup="false"private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);}只有AutoEventWireup="true"才能自动加载数据,