税程财经网
您的当前位置:首页asp.net中Datagrid分页用法

asp.net中Datagrid分页用法

来源:税程财经网


接上
/**//*
*计算总记录的静态函数
*本人在这里使用静态函数的理由是:如果引用的是静态数据或静态函数,连接器会优化生成代码,去掉动态重定位项(对

海量数据表分页效果更明显)。
*希望大家给予意见、如有不正确的地方望指正。
*/
public static int Calc()
{
int RecordCount = 0;
SqlCommand MyCmd = new SqlCommand("select count(*) as co from fdcproject",MyCon());
SqlDataReader dr = MyCmd.ExecuteReader();
if(dr.Read())
RecordCount = Int32.Parse(dr["co"].ToString());
MyCmd.Connection.Close();
return RecordCount;
}

//数据库连接语句(从Web.Config中获取)
public static SqlConnection MyCon()
{
SqlConnection MyConnection = new SqlConnection(ConfigurationSettings.AppSettings["DSN2"]);
MyConnection.Open();
return MyConnection;
}

private void TDataBind()
{
CurrentPage = (int)ViewState["PageIndex"];//从ViewState中读取页码值保存到CurrentPage变量中进行按钮失效运算
Pages = (int)ViewState["PageCounts"];//从ViewState中读取总页参数进行按钮失效运算
//判断四个按钮(首页、上一页、下一页、尾页)状态
if (CurrentPage + 1 > 1)
{
Fistpage.Enabled = true;
Prevpage.Enabled = true;
}
else
{
Fistpage.Enabled = false;
Prevpage.Enabled = false;
}
if (CurrentPage == Pages)
{
Nextpage.Enabled = false;
Lastpage.Enabled = false;
}
else
{
Nextpage.Enabled = true;
Lastpage.Enabled = true;
}
//数据绑定到DataList控件
DataSet ds = new DataSet();
//核心SQL语句,进行查询运算(决定了分页的效率:))
SqlDataAdapter MyAdapter = new SqlDataAdapter("Select Top "+PageSize+" * from fdcproject where projid not in(select top "+PageSize*CurrentPage+" projid from fdcproject order by projid asc) order by projid asc",MyCon());
MyAdapter.Fill(ds,"news");
datalist1.DataSource = ds.Tables["news"].DefaultView;
datalist1.DataBind();
//显示Label控件LCurrentPaget和文本框控件gotoPage状态
LCurrentPage.Text = (CurrentPage+1).ToString();
gotoPage.Text = (CurrentPage+1).ToString();
//释放SqlDataAdapter
MyAdapter.Dispose();
}
显示全文