在DataGrid中实现分页
用过ASP的朋友也许对起分页感到头大,但在ASP。NET中其自动分页功能便让您能得心应手。这是前台代码:
后台代码如下:
就怎么如次 easy 一个简单的分页便实现了。
后台代码如下:
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 mynew
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
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.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
conn.Open();
SqlDataAdapter myAdapter=new SqlDataAdapter();
myAdapter.SelectCommand=new SqlCommand("select CategoryID,CategoryName from Categories",conn);
// myAdapter.SelectCommand.CommandType=CommandType.StoredProcedure;
DataSet ds=new DataSet();
myAdapter.Fill(ds,"Categories");
MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();
lblCurrentIndex.Text="第"+((Int32)MyDataGrid.CurrentPageIndex+1)+"页";
lblPageCount.Text="/共"+MyDataGrid.PageCount+"页";
conn.Close();
}
Web 窗体设计器生成的代码
private void PagerButton_Click(object sender, System.EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument;
//获取LinkButton的参数值
switch(arg)
{
case ("next"):
if(MyDataGrid.CurrentPageIndex<(MyDataGrid.PageCount-1))
MyDataGrid.CurrentPageIndex++;
break;
case("prev"):
if(MyDataGrid.CurrentPageIndex>0)
MyDataGrid.CurrentPageIndex--;
break;
case("first"):
MyDataGrid.CurrentPageIndex=0;
break;
case("last"):
MyDataGrid.CurrentPageIndex=MyDataGrid.PageCount-1;
break;
}
MyDataGrid.DataBind();
}
}
}
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 mynew
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
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.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
conn.Open();
SqlDataAdapter myAdapter=new SqlDataAdapter();
myAdapter.SelectCommand=new SqlCommand("select CategoryID,CategoryName from Categories",conn);
// myAdapter.SelectCommand.CommandType=CommandType.StoredProcedure;
DataSet ds=new DataSet();
myAdapter.Fill(ds,"Categories");
MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();
lblCurrentIndex.Text="第"+((Int32)MyDataGrid.CurrentPageIndex+1)+"页";
lblPageCount.Text="/共"+MyDataGrid.PageCount+"页";
conn.Close();
}
Web 窗体设计器生成的代码
private void PagerButton_Click(object sender, System.EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument;
//获取LinkButton的参数值
switch(arg)
{
case ("next"):
if(MyDataGrid.CurrentPageIndex<(MyDataGrid.PageCount-1))
MyDataGrid.CurrentPageIndex++;
break;
case("prev"):
if(MyDataGrid.CurrentPageIndex>0)
MyDataGrid.CurrentPageIndex--;
break;
case("first"):
MyDataGrid.CurrentPageIndex=0;
break;
case("last"):
MyDataGrid.CurrentPageIndex=MyDataGrid.PageCount-1;
break;
}
MyDataGrid.DataBind();
}
}
}
就怎么如次 easy 一个简单的分页便实现了。