ASP.NET GridView分页及排序 源代码
//.cs 文件部分代码
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;
public partial class BasicInfo_Product : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//绑定数据
strQuery = "SELECT * FROM JXNF_PRODUCT ";
DataView dv = PlatForm.NamedObjects.NonTransactionDatabaseAgent.ExecuteDataView(strQuery);
dv.Sort = "PRODUCTID DESC";
GridViewMain.PageIndex = Convert.ToInt32(this.Session["page"]);
GridViewMain.DataSource = dv;
GridViewMain.DataBind();
this.ViewState["sort"] = "PRODUCTID";
this.ViewState["sortDerect"] = "DESC";
this.ViewState["strSql"] = strQuery;
}
}
protected void LinkButtongo_Click(object sender, EventArgs e)
{
//跳转
String strQuery = this.ViewState["strSql"].ToString();
TextBox textBoxpage = (TextBox)GridViewMain.BottomPagerRow.Cells[0].FindControl("TextBoxpage");
if (textBoxpage.Text == "")
{
GridViewMain.PageIndex = 0;
}
else
{
try
{
GridViewMain.PageIndex = Convert.ToInt32(textBoxpage.Text) - 1;
if (GridViewMain.PageIndex < 0)
{
GridViewMain.PageIndex = 0;
}
}
catch
{
GridViewMain.PageIndex = 0; } }
this.Session["page"] = GridViewMain.PageIndex;
DataSet dataSet = PlatForm.NamedObjects.NonTransactionDatabaseAgent.ExecuteDataSet(strQuery);
DataView dv = new DataView(dataSet.Tables[0]);
dv.Sort=this.ViewState["sort"].ToString()+" "+this.ViewState["sortDerect"].ToString();
GridViewMain.DataSource = dv;
GridViewMain.DataBind();
if (GridViewMain.PageIndex == 0)
{
LinkButton lb = (LinkButton)GridViewMain.BottomPagerRow.Cells[0].FindControl("LinkButtonupper");
lb.Visible = false;
}
}
protected void LinkButtonfirst_Click(object sender, EventArgs e)
{
//首页
String strSql = this.ViewState["strSql"].ToString();
GridViewMain.PageIndex = 0;
this.Session["page"] = GridViewMain.PageIndex;
DataSet dataSet = PlatForm.NamedObjects.NonTransactionDatabaseAgent.ExecuteDataSet(strSql);
DataView dv = new DataView(dataSet.Tables[0]);
dv.Sort=this.ViewState["sort"].ToString()+" "+this.ViewState["sortDerect"].ToString();
GridViewMain.DataSource = dv;
GridViewMain.DataBind();
LinkButton lb = (LinkButton)GridViewMain.BottomPagerRow.Cells[0].FindControl("LinkButtonupper");
lb.Visible = false;
}
protected void LinkButtonupper_Click(object sender, EventArgs e)
{
//上一页
if (GridViewMain.PageIndex > 0)
{
GridViewMain.PageIndex--;
this.Session["page"] = GridViewMain.PageIndex;
String strSql = this.ViewState["strSql"].ToStrin