其实满简单的.就是前后页分清楚就可以了.做出个现成的例子,以后用就可以直接copy.省的再想.提高工作效率
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;
using System.Net;
public partial class dataGridChange : System.Web.UI.Page
... {
int index;
protected void Page_Load(object sender, EventArgs e)
...{
string localName = Dns.GetHostName();
IPHostEntry localIP=Dns.GetHostByName(localName);
// string id=localIP.AddressList[i].ToString();
bind();
}
private void bind()
...{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
SqlDataAdapter sda = new SqlDataAdapter("select * from Employees ",con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataGrid1.DataKeyField = "EmployeeID";
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
show();
}
protected void changePage_buttonclick(object sender, EventArgs e)
...{
string arg = ((LinkButton)sender).CommandName.ToString();
switch(arg)
...{
case "pro":
if (DataGrid1.CurrentPageIndex > 0)
DataGrid1.CurrentPageIndex -= 1;
break;
case "next":
if (DataGrid1.CurrentPageIndex <(DataGrid1.PageCount - 1))
DataGrid1.CurrentPageIndex += 1;
break;
case "last":
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1;
break;
default:
// DataGrid1.CurrentPageIndex = System.Convert.ToInt32(arg);
DataGrid1.CurrentPageIndex = 0;
break;
}
show();
bind();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
...{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
show();
bind();
}
public void show()
...{
lblCurrentIndex.Text = "当前第"+(DataGrid1.CurrentPageIndex+1).ToString()+"页";
lblCount.Text = "总共"+DataGrid1.PageCount.ToString()+"页";
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
...{
int empID=(int)DataGrid1.DataKeys[e.Item.ItemIndex];
string sqlcom = "delete Employees where EmployeeID="+empID;
try
...{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
SqlCommand comm = new SqlCommand(sqlcom,conn);
conn.Open();
comm.ExecuteNonQuery();
index = DataGrid1.CurrentPageIndex; //取得当前页的索引
if (DataGrid1.PageCount - DataGrid1.CurrentPageIndex == 1 && DataGrid1.Items.Count == 1)//如果有多页,并且当前页中的数据仅有一项
...{
if (DataGrid1.PageCount > 1)
...{
index =index- 1;
}
else
...{
index = 0;
}
}
DataGrid1.CurrentPageIndex = index;
bind();
conn.Close();
}
catch(Exception a)
...{
}
}
}
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;
using System.Net;
public partial class dataGridChange : System.Web.UI.Page
... {
int index;
protected void Page_Load(object sender, EventArgs e)
...{
string localName = Dns.GetHostName();
IPHostEntry localIP=Dns.GetHostByName(localName);
// string id=localIP.AddressList[i].ToString();
bind();
}
private void bind()
...{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
SqlDataAdapter sda = new SqlDataAdapter("select * from Employees ",con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataGrid1.DataKeyField = "EmployeeID";
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
show();
}
protected void changePage_buttonclick(object sender, EventArgs e)
...{
string arg = ((LinkButton)sender).CommandName.ToString();
switch(arg)
...{
case "pro":
if (DataGrid1.CurrentPageIndex > 0)
DataGrid1.CurrentPageIndex -= 1;
break;
case "next":
if (DataGrid1.CurrentPageIndex <(DataGrid1.PageCount - 1))
DataGrid1.CurrentPageIndex += 1;
break;
case "last":
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1;
break;
default:
// DataGrid1.CurrentPageIndex = System.Convert.ToInt32(arg);
DataGrid1.CurrentPageIndex = 0;
break;
}
show();
bind();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
...{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
show();
bind();
}
public void show()
...{
lblCurrentIndex.Text = "当前第"+(DataGrid1.CurrentPageIndex+1).ToString()+"页";
lblCount.Text = "总共"+DataGrid1.PageCount.ToString()+"页";
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
...{
int empID=(int)DataGrid1.DataKeys[e.Item.ItemIndex];
string sqlcom = "delete Employees where EmployeeID="+empID;
try
...{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
SqlCommand comm = new SqlCommand(sqlcom,conn);
conn.Open();
comm.ExecuteNonQuery();
index = DataGrid1.CurrentPageIndex; //取得当前页的索引
if (DataGrid1.PageCount - DataGrid1.CurrentPageIndex == 1 && DataGrid1.Items.Count == 1)//如果有多页,并且当前页中的数据仅有一项
...{
if (DataGrid1.PageCount > 1)
...{
index =index- 1;
}
else
...{
index = 0;
}
}
DataGrid1.CurrentPageIndex = index;
bind();
conn.Close();
}
catch(Exception a)
...{
}
}
}
html代码:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
dataGridChange.aspx.cs
"
Inherits
=
"
dataGridChange
"
%>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
& nbsp; & nbsp;
</ div >
< table style = " width: 618px; position: static; height: 195px " >
< tr >
< td style = " width: 100px " >
</ td >
</ tr >
< tr >
< td style = " width: 100px " align = " right " >
< asp:DataGrid ID = " DataGrid1 " runat = " server " Style = " position: static; " AutoGenerateColumns = " False " Height = " 179px " OnPageIndexChanged = " DataGrid1_PageIndexChanged " Width = " 639px " AllowPaging = " True " PageSize = " 3 " OnDeleteCommand = " DataGrid1_DeleteCommand " >
< Columns >
< asp:BoundColumn DataField = " EmployeeID " HeaderText = " EmployeeID " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " LastName " HeaderText = " LastName " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " FirstName " HeaderText = " FirstName " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " Title " HeaderText = " Title " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " City " HeaderText = " City " ></ asp:BoundColumn >
< asp:ButtonColumn CommandName = " Delete " Text = " 删除 " ></ asp:ButtonColumn >
</ Columns >
< PagerStyle Visible = " False " />
</ asp:DataGrid >
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; < asp:Label ID = " lblCurrentIndex "
runat = " server " Style = " position: static " Text = "" ></ asp:Label >
& nbsp; & nbsp; & nbsp;
< asp:Label ID = " lblCount " runat = " server " Style = " position: static " ></ asp:Label >& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
< asp:LinkButton ID = " firstBtn " runat = " server "
CommandName = " first " OnClick = " changePage_buttonclick " Style = " position: static " > 首页 </ asp:LinkButton >
< asp:LinkButton ID = " proBtn " runat = " server " CommandName = " pro " Style = " position: static " OnClick = " changePage_buttonclick " > 上一页 </ asp:LinkButton >
< asp:LinkButton ID = " nextBtn " runat = " server " CommandName = " next " Style = " position: static " OnClick = " changePage_buttonclick " > 下一页 </ asp:LinkButton >
< asp:LinkButton ID = " lastBtn " runat = " server " CommandName = " last " Style = " position: static " OnClick = " changePage_buttonclick " > 尾页 </ asp:LinkButton >
</ td >
</ tr >
< tr >
< td style = " width: 100px; height: 61px " >
& nbsp; & nbsp; & nbsp;
</ td >
</ tr >
</ table >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
& nbsp; & nbsp;
</ div >
< table style = " width: 618px; position: static; height: 195px " >
< tr >
< td style = " width: 100px " >
</ td >
</ tr >
< tr >
< td style = " width: 100px " align = " right " >
< asp:DataGrid ID = " DataGrid1 " runat = " server " Style = " position: static; " AutoGenerateColumns = " False " Height = " 179px " OnPageIndexChanged = " DataGrid1_PageIndexChanged " Width = " 639px " AllowPaging = " True " PageSize = " 3 " OnDeleteCommand = " DataGrid1_DeleteCommand " >
< Columns >
< asp:BoundColumn DataField = " EmployeeID " HeaderText = " EmployeeID " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " LastName " HeaderText = " LastName " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " FirstName " HeaderText = " FirstName " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " Title " HeaderText = " Title " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " City " HeaderText = " City " ></ asp:BoundColumn >
< asp:ButtonColumn CommandName = " Delete " Text = " 删除 " ></ asp:ButtonColumn >
</ Columns >
< PagerStyle Visible = " False " />
</ asp:DataGrid >
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; < asp:Label ID = " lblCurrentIndex "
runat = " server " Style = " position: static " Text = "" ></ asp:Label >
& nbsp; & nbsp; & nbsp;
< asp:Label ID = " lblCount " runat = " server " Style = " position: static " ></ asp:Label >& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
< asp:LinkButton ID = " firstBtn " runat = " server "
CommandName = " first " OnClick = " changePage_buttonclick " Style = " position: static " > 首页 </ asp:LinkButton >
< asp:LinkButton ID = " proBtn " runat = " server " CommandName = " pro " Style = " position: static " OnClick = " changePage_buttonclick " > 上一页 </ asp:LinkButton >
< asp:LinkButton ID = " nextBtn " runat = " server " CommandName = " next " Style = " position: static " OnClick = " changePage_buttonclick " > 下一页 </ asp:LinkButton >
< asp:LinkButton ID = " lastBtn " runat = " server " CommandName = " last " Style = " position: static " OnClick = " changePage_buttonclick " > 尾页 </ asp:LinkButton >
</ td >
</ tr >
< tr >
< td style = " width: 100px; height: 61px " >
& nbsp; & nbsp; & nbsp;
</ td >
</ tr >
</ table >
</ form >
</ body >
</ html >