using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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.Data.SqlClient;
using System.Configuration;
namespace MyCommerce
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public enum GridNavigation:int
{
First = -2,
Prevous = -1,
Next = 1,
Last=2
}
public GridNavigation Direction=GridNavigation.Next;
private string queryStr_Pre="select * from (select top {0} * from CMRC_Customers where CustomerID < {1} order by CustomerID desc) as t order by CustomerID asc";
protected System.Web.UI.WebControls.Button Btn_Pre;
protected System.Web.UI.WebControls.Button Btn_Next;
private string queryStr_Next="select top {0} * from CMRC_Customers where CustomerID > {1} ";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
ViewState["FirstKey"]=0;
ViewState["LastKey"]=0;
SetVirtualPage();
DataGrid1.CurrentPageIndex=0;
DataGrid1_DataBind();
}
}
private void DataGrid1_DataBind()
{
if(DataGrid1.CurrentPageIndex == 0)
{
Btn_Pre.Enabled=false;
}
else
{
Btn_Pre.Enabled=true;
}
if(DataGrid1.CurrentPageIndex == Convert.ToInt32(ViewState["TotalPage"]))
{
Btn_Next.Enabled=false;
}
else
{
Btn_Next.Enabled=true;
}
SqlDataReader dr=GetDataSource();
DataGrid1.DataSource=dr;
DataGrid1.DataBind();
dr.Close();
ViewState["FirstKey"]=DataGrid1.DataKeys[0];
ViewState["LastKey"]=DataGrid1.DataKeys[DataGrid1.Items.Count-1];
}
private SqlDataReader GetDataSource()
{
string cmd_Text = "";
switch(Direction)
{
case GridNavigation.Next:
cmd_Text=string.Format(queryStr_Next,DataGrid1.PageSize,Convert.ToInt32(ViewState["LastKey"]));
break;
case GridNavigation.Prevous:
cmd_Text=string.Format(queryStr_Pre,DataGrid1.PageSize,Convert.ToInt32(ViewState["FirstKey"]));
break;
}
SqlDataReader dr = SqlHelper.GetDataSourceByPageIndex(cmd_Text);
return dr;
}
private void SetVirtualPage()
{
SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
SqlCommand cmd=new SqlCommand("select count(*) from CMRC_Customers",cn);
cn.Open();
DataGrid1.VirtualItemCount=(int)cmd.ExecuteScalar();
ViewState["TotalPage"]=(DataGrid1.VirtualItemCount / 10);
cn.Close();
return ;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Btn_Pre.Click += new System.EventHandler(this.Btn_Pre_Click);
this.Btn_Next.Click += new System.EventHandler(this.Btn_Next_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
// private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
// {
// switch(e.CommandArgument.ToString())
// {
// case DataGrid.NextPageCommandArgument:Direction=GridNavigation.Next;break;
// case DataGrid.PrevPageCommandArgument:Direction=GridNavigation.Prevous;break;
//
// }
// }
private void Btn_Pre_Click(object sender, System.EventArgs e)
{
DataGrid1.CurrentPageIndex -= 1;
Direction = GridNavigation.Prevous;
DataGrid1_DataBind();
}
private void Btn_Next_Click(object sender, System.EventArgs e)
{
DataGrid1.CurrentPageIndex += 1;
Direction = GridNavigation.Next;
DataGrid1_DataBind();
}
}
}