下面这段代码,点击下一页和上一页的时候,总是要跳过一页。比如,当前显示第5页,我点上一页时,跳到第3也。何解?在线等!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.OleDb;
using System.Configuration;namespace WebApplication1
{
/// <summary>
/// showtousu 的摘要说明。
/// </summary>
///
public class showtousu : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
DataSet();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnFirst.Click += new System.EventHandler(this.PagerButtonClick);
this.btnPrev.Click += new System.EventHandler(this.PagerButtonClick);
this.btnNext.Click += new System.EventHandler(this.PagerButtonClick);
this.btnLast.Click += new System.EventHandler(this.PagerButtonClick);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataSet()
{
OleDbConnection objConnection = new OleDbConnection(ConfigurationSettings.AppSettings["strConnection"]); string strSQL = "select * from tousu order by ID desc"; DataSet objDataSet = new DataSet(); OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL,objConnection); objAdapter.Fill(objDataSet,"tousu"); if (objDataSet.Tables["tousu"].Rows.Count == 0)
{
Response.Write("sdf");
Response.End();
} for (int i=0;i<objDataSet.Tables["tousu"].Rows.Count;i++)
{
objDataSet.Tables["tousu"].Rows[i]["zhuti"] = "<a href=\"tousuchakan.aspx?ID=" + objDataSet.Tables["tousu"].Rows[i]["ID"].ToString() + "\">" + GetChar(objDataSet.Tables["tousu"].Rows[i]["zhuti"].ToString(),20) + "</a> " + objDataSet.Tables["tousu"].Rows[i]["times"].ToString();
} DataGrid1.DataSource = objDataSet.Tables["tousu"].DefaultView;
DataGrid1.DataBind();
ShowStates(); }
public string GetChar( string str , int len )
{
if( str.Length > len )
{
str = str.Substring( 0, len );
str = str + "..";
return str;
}
else
{
return str;
}
} public void PagerButtonClick(object sender,EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (DataGrid1.CurrentPageIndex<DataGrid1.PageCount)
{
DataGrid1.CurrentPageIndex=DataGrid1.CurrentPageIndex+1;
}
break;
case "prev":
if (DataGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex-=1;
}
break; case "last":
DataGrid1.CurrentPageIndex=(DataGrid1.PageCount-1);
break;
default:
DataGrid1.CurrentPageIndex=System.Convert.ToInt32(arg);
break;
}
OleDbConnection objConnection = new OleDbConnection(ConfigurationSettings.AppSettings["strConnection"]); string strSQL = "select * from tousu order by ID desc"; DataSet objDataSet = new DataSet(); OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL,objConnection); objAdapter.Fill(objDataSet,"tousu"); DataGrid1.DataSource = objDataSet.Tables["tousu"].DefaultView;
DataGrid1.DataBind();
ShowStates(); } private void ShowStates()
{
lblPageCount.Text="总共" + DataGrid1.PageCount.ToString() + "页";
lblCurrentIndex.Text="第" + (DataGrid1.CurrentPageIndex+1).ToString() + "页";
}
}
}
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.OleDb;
using System.Configuration;namespace WebApplication1
{
/// <summary>
/// showtousu 的摘要说明。
/// </summary>
///
public class showtousu : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
DataSet();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnFirst.Click += new System.EventHandler(this.PagerButtonClick);
this.btnPrev.Click += new System.EventHandler(this.PagerButtonClick);
this.btnNext.Click += new System.EventHandler(this.PagerButtonClick);
this.btnLast.Click += new System.EventHandler(this.PagerButtonClick);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataSet()
{
OleDbConnection objConnection = new OleDbConnection(ConfigurationSettings.AppSettings["strConnection"]); string strSQL = "select * from tousu order by ID desc"; DataSet objDataSet = new DataSet(); OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL,objConnection); objAdapter.Fill(objDataSet,"tousu"); if (objDataSet.Tables["tousu"].Rows.Count == 0)
{
Response.Write("sdf");
Response.End();
} for (int i=0;i<objDataSet.Tables["tousu"].Rows.Count;i++)
{
objDataSet.Tables["tousu"].Rows[i]["zhuti"] = "<a href=\"tousuchakan.aspx?ID=" + objDataSet.Tables["tousu"].Rows[i]["ID"].ToString() + "\">" + GetChar(objDataSet.Tables["tousu"].Rows[i]["zhuti"].ToString(),20) + "</a> " + objDataSet.Tables["tousu"].Rows[i]["times"].ToString();
} DataGrid1.DataSource = objDataSet.Tables["tousu"].DefaultView;
DataGrid1.DataBind();
ShowStates(); }
public string GetChar( string str , int len )
{
if( str.Length > len )
{
str = str.Substring( 0, len );
str = str + "..";
return str;
}
else
{
return str;
}
} public void PagerButtonClick(object sender,EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (DataGrid1.CurrentPageIndex<DataGrid1.PageCount)
{
DataGrid1.CurrentPageIndex=DataGrid1.CurrentPageIndex+1;
}
break;
case "prev":
if (DataGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex-=1;
}
break; case "last":
DataGrid1.CurrentPageIndex=(DataGrid1.PageCount-1);
break;
default:
DataGrid1.CurrentPageIndex=System.Convert.ToInt32(arg);
break;
}
OleDbConnection objConnection = new OleDbConnection(ConfigurationSettings.AppSettings["strConnection"]); string strSQL = "select * from tousu order by ID desc"; DataSet objDataSet = new DataSet(); OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL,objConnection); objAdapter.Fill(objDataSet,"tousu"); DataGrid1.DataSource = objDataSet.Tables["tousu"].DefaultView;
DataGrid1.DataBind();
ShowStates(); } private void ShowStates()
{
lblPageCount.Text="总共" + DataGrid1.PageCount.ToString() + "页";
lblCurrentIndex.Text="第" + (DataGrid1.CurrentPageIndex+1).ToString() + "页";
}
}
}
解决方案 »
- 图片为什么不能显示啊?貌似没错啊!!
- 使用Session记录用户登录情况的问题
- Excel导入的过程中如何按照sql2000中设定的数据类型和长度进行导入
- MIS系统统计部分怎么做
- repeater控件小问题
- datagrid 中复选得处理
- 大家看看为什么这段数据不能绑定到下拉列表框啊,,,,,, 谢谢了,
- 关于VIEWSTATE的问题,请高手指点
- 关于用户控件的问题~
- 调查:大家用ASP.NET准备用哪种语言,C# or VB7 or Javascript.
- 我在一个Table控件中的每个CELL中都加了一个TEXTBOX,请问......
- setTimeOut里面能调用setTimeOut()吗?,有什么好办法?来者有分。。。。
protected void BindGrid()
{
string strSelect = "SELECT * from t_department";
data.CommandString = strSelect;
DataView dv = new DataView();
dv = data.GetDataView();
if(dv.Count>0)
{
GrdDep.DataSource = dv;
GrdDep.DataBind();
LabOK.Text = "共 " + dv.Count + " 条信息 ";
LabOK.Text += "分 " + GrdDep.PageCount + " 页显示 ";
LabOK.Text += GrdDep.PageSize + " 条/页 ";
}
else
{
LabOK.Text="对不起,暂时尚没有数据。";
}
}下面是重要的有关分页的:protected void GrdDep_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
GrdDep.CurrentPageIndex = e.NewPageIndex;
Control_Load();
GrdDep.DataBind();
}这个市必不可少的翻页事件,你试试,可以问我
http://www.dotnetjunkies.com/Tutorial/EA868776-D71E-448A-BC23-B64B871F967F.dcik
http://www.dotnetbips.com/displayarticle.aspx?id=138
datakeyfield="room_id" bordercolor="#CCCCCC" onpageindexchanged="grdRooms_PageIndexChanged"
borderstyle="None" borderwidth="1px" backcolor="White" cellpadding="3">
<selecteditemstyle font-bold="True" forecolor="White" cssclass="Grid_SelectedItem" backcolor="#669999"></selecteditemstyle>
<alternatingitemstyle cssclass="Grid_AlternatingItem"></alternatingitemstyle>
<itemstyle forecolor="#000066" cssclass="Grid_Item"></itemstyle>
<headerstyle font-bold="True" forecolor="White" cssclass="Grid_Header" backcolor="#006699"></headerstyle>
<footerstyle forecolor="#000066" backcolor="White"></footerstyle>
<columns>
<asp:boundcolumn visible="False" datafield="room_id"></asp:boundcolumn>
<asp:boundcolumn datafield="building_name" headertext="所属公寓楼"></asp:boundcolumn>
<asp:buttoncolumn text="选择" datatextfield="room_code" headertext="选择房间" commandname="Select"></asp:buttoncolumn>
<asp:boundcolumn datafield="room_type" headertext="房间类型"></asp:boundcolumn>
<asp:boundcolumn datafield="floor_num" headertext="楼层"></asp:boundcolumn>
<asp:boundcolumn datafield="bed_sum" headertext="床位数"></asp:boundcolumn>
<asp:boundcolumn datafield="free_bed_sum" headertext="空床位数"></asp:boundcolumn>
</columns>
<pagerstyle horizontalalign="Right" forecolor="#000066" backcolor="White" mode="NumericPages"></pagerstyle>
</asp:datagrid>
但是分页的代码应该是:protected void GrdDep_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
GrdDep.EditItemIndex = -1;
GrdDep.CurrentPageIndex = e.NewPageIndex;
BindGrid();//重新帮定DataGrid
}
因为自动分页的效率太低,对服务器资源占用太大,想想,要是有100万条记录的个大表,用自动分页,会是什么样子?
__________________
datagrid的自定义分页也可以只取当前页的数据的
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.Common;
using System.Data.SqlClient;namespace WebDataGridPage
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataGrid1.AllowPaging = true;
DataGrid1.AllowCustomPaging = true; DataGrid1.PageSize = 20;
DataGrid1.VirtualItemCount = 255634; //GetProductCount(); if(!this.IsPostBack)
{
DataGrid1.DataSource = ListProduct(0, DataGrid1.PageSize);
DataGrid1.DataBind();
}
} public DataTable ListProduct(int pageIndex, int pageSize)
{
SqlConnection conn = new SqlConnection("server=acmesql;uid=sa;pwd=;database=apcb");
//SqlCommand comm = new SqlCommand("usp_speed3 "+ pageIndex +", "+ pageSize ,conn);
SqlDataAdapter sda = new SqlDataAdapter("usp_speed3 "+ pageIndex +", "+ pageSize,conn);
conn.Open();
//comm.ExecuteNonQuery();
DataSet ds = new DataSet();
sda.Fill(ds,"a");
return(ds.Tables["a"]);
conn.Close();
} private void GetProductCount()
{
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
// 如果在存储过程分页功能中用1表示第一页的序号的话那么这里必须用e.NewPageIndex+1作为pageIndex(如果选择了DataGrid上页号为3的页,那么e.NewPageIndex就为2), 否则的话直接用e.NewPageIndex就可以了
DataGrid1.DataSource = ListProduct(e.NewPageIndex+1, DataGrid1.PageSize); // 从数据库中读取新的数据
DataGrid1.DataBind();
// 设置当前的Page序号值, 如果不设置的话它是不会变得, 这样会给造成误解,以为所有页的数据相同。
DataGrid1.CurrentPageIndex =e.NewPageIndex;
} private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Pager)
{
return;
}
}
}
}/*
* 创建存储过程实现查询
CREATE PROCEDURE usp_speed3
@nextid int,
@pagesize int
AS
set nocount on
declare @sql nvarchar(2000) set @sql=N'select top '+convert(varchar(12),@pagesize)+
' Lotnum from LotInfo where lotnum not in (select top ' + convert(varchar(12),@nextid) +
' Lotnum from LotInfo order by LotNum) order by LotNum'
exec sp_executesql @sql
GO
*/
case "next":
if (DataGrid1.CurrentPageIndex<DataGrid1.PageCount)
{
DataGrid1.CurrentPageIndex=DataGrid1.CurrentPageIndex+1;
}
改为
case "next":
if (DataGrid1.CurrentPageIndex<DataGrid1.PageCount-1)
{
DataGrid1.CurrentPageIndex=DataGrid1.CurrentPageIndex+1;
}至于你错误的原因,就你现在给的代码,呵呵还是没找着