定位到下一页
当用户定位到下一页时,您要再次使用一般方法执行 cmdNext 命令。这一次,命令的参数是网格中最后一行 ID 列的值。您还需要将页码加 1,以便一般 FillGrid 方法可以使用该数字为该 ID 创建另一个视图状态项。若要将页码加 1,您需要从视图状态中获取旧页码。当进行该操作时,需要将其强制转换为一个整数,因为视图状态中的所有项都存储为对象。“下一页”按钮的代码如下所示:' Visual Basic
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
' Get the page number of the page most recently displayed
CurrentPage = CType(ViewState("CurrentPage"), Integer)
CurrentPage += 1
'Gets the id on current page
Dim lastid As String = DataGrid1.Items(9).Cells(0).Text
cmdNext.Parameters("@customerid").Value = lastid
FillGrid(cmdNext)
End Sub//C#
private void btnNext_Click(object sender, System.EventArgs e)
{
// Get the page number of the page most recently displayed
CurrentPage = (int)(ViewState["CurrentPage"]);
CurrentPage++;
// Gets the id on current page
string lastid = DataGrid1.Items[9].Cells[0].Text;
cmdNext.Parameters["@customerid"].Value = lastid;
FillGrid(cmdNext);
}
定位到上一页
最后,您需要添加向后移动的代码。这里将使用您存储在视图状态中的信息。首先,获取当前页码然后减 1。然后使用该页码作为名称查找视图状态,获取页上第一行的 ID。使用该 ID 作为 cmdPrevious 命令(将该命令传递给一般 FillGrid 方法)的参数。为了防止离开表的开头,您应该检查页计数不在零以下。另外,您应该启用“下一页”按钮,以防它在上一次定位中被禁用。' Visual Basic
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
btnNext.Enabled = True
CurrentPage = CType(ViewState("CurrentPage"), Integer)
CurrentPage -= 1
If CurrentPage >= 0 Then
Dim firstid As String
firstid = CType(ViewState(CurrentPage.ToString), String)
cmdPrevious.Parameters("@customerid").Value = firstid
FillGrid(cmdPrevious)
End If
End Sub//C#
private void btnPrevious_Click(object sender, System.EventArgs e)
{
btnNext.Enabled = true;
CurrentPage = (int)(ViewState["CurrentPage"]);
CurrentPage--;
if (CurrentPage >= 0)
{
string firstid;
firstid = (string)(ViewState[CurrentPage.ToString()]);
cmdPrevious.Parameters["@customerid"].Value = firstid;
FillGrid(cmdPrevious);
}
}
当用户定位到下一页时,您要再次使用一般方法执行 cmdNext 命令。这一次,命令的参数是网格中最后一行 ID 列的值。您还需要将页码加 1,以便一般 FillGrid 方法可以使用该数字为该 ID 创建另一个视图状态项。若要将页码加 1,您需要从视图状态中获取旧页码。当进行该操作时,需要将其强制转换为一个整数,因为视图状态中的所有项都存储为对象。“下一页”按钮的代码如下所示:' Visual Basic
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
' Get the page number of the page most recently displayed
CurrentPage = CType(ViewState("CurrentPage"), Integer)
CurrentPage += 1
'Gets the id on current page
Dim lastid As String = DataGrid1.Items(9).Cells(0).Text
cmdNext.Parameters("@customerid").Value = lastid
FillGrid(cmdNext)
End Sub//C#
private void btnNext_Click(object sender, System.EventArgs e)
{
// Get the page number of the page most recently displayed
CurrentPage = (int)(ViewState["CurrentPage"]);
CurrentPage++;
// Gets the id on current page
string lastid = DataGrid1.Items[9].Cells[0].Text;
cmdNext.Parameters["@customerid"].Value = lastid;
FillGrid(cmdNext);
}
定位到上一页
最后,您需要添加向后移动的代码。这里将使用您存储在视图状态中的信息。首先,获取当前页码然后减 1。然后使用该页码作为名称查找视图状态,获取页上第一行的 ID。使用该 ID 作为 cmdPrevious 命令(将该命令传递给一般 FillGrid 方法)的参数。为了防止离开表的开头,您应该检查页计数不在零以下。另外,您应该启用“下一页”按钮,以防它在上一次定位中被禁用。' Visual Basic
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
btnNext.Enabled = True
CurrentPage = CType(ViewState("CurrentPage"), Integer)
CurrentPage -= 1
If CurrentPage >= 0 Then
Dim firstid As String
firstid = CType(ViewState(CurrentPage.ToString), String)
cmdPrevious.Parameters("@customerid").Value = firstid
FillGrid(cmdPrevious)
End If
End Sub//C#
private void btnPrevious_Click(object sender, System.EventArgs e)
{
btnNext.Enabled = true;
CurrentPage = (int)(ViewState["CurrentPage"]);
CurrentPage--;
if (CurrentPage >= 0)
{
string firstid;
firstid = (string)(ViewState[CurrentPage.ToString()]);
cmdPrevious.Parameters["@customerid"].Value = firstid;
FillGrid(cmdPrevious);
}
}
解决方案 »
- 一个关于http请求的问题
- RDLC报表如何实现套打?
- asp.net使用dotnetopenmail发送邮件出现奇怪问题,请教高手解答!!!
- 帮忙解决一个思路问题
- 急!!求达人帮助!asp.net 读取Excel,如何根据列头获得该列头的位置?
- 怎么样把一个javascript变量保存在SESSION中
- 请教:新手上路,在线等
- 在VS2005中如何处理好 DataSource 与使用 Dao 层之间的矛盾
- 如何使用response.redirect("xxxx.aspx")在新的空白页中打开xxxx.aspx??
- 为什么ImageButton总是运行2次事件处理函数?
- 关于DataGrid的问题(在线等待.......)
- 300分送上 !!!简单问题!
To: redcaff_l(热的咖啡)
没有错呀。我的意思是一次只取10条记录出来,每翻页一次再重新取,
怎么思路我也知道,但是我就是找不到最后的一页。你说总数可以先将总数得到,然后除与10,整数就是最后一页了,这是没有错。
但是用SQL语句怎么写法呢想不通!
To:bluefish922(一天到晚游泳的鱼)
我想你应该给我详细一点的答复。如果用其他方法,我也知道,但是我偏偏喜欢用DataGrid来写呀·‘’‘’‘’‘’‘’‘’‘’
this.cmdNext.CommandText = "SELECT TOP 10 CustomerID, CompanyName, City FROM Customers WHERE (CustomerID > @customerid) ORDER BY CustomerID";
-- upAccountSpecificationGetList_ByPage
-------------------------------------------------------------------------------
CREATE PROCEDURE upAccountSpecificationGetList_ByPage
(
@nPageSize int ,
@nCurrentPage int,
@totalNumResults int output
)
AS -- we are creating a temporary table to store the currently
-- selected page of data. a rowid field has been added to allow
-- us to track which page we are on (the itemid didn't work
-- in this case because it is a character data type and it is
-- much easier to calculate the paging with an int)
CREATE TABLE #SearchResultsTempTable
(
ROWID int IDENTITY PRIMARY KEY,
ROLENAME varchar(100) NOT NULL ,
DESCRIPTION varchar(255) NULL,
STATE varchar(10)
)
-- copy the search results into the temporary table
INSERT INTO #SearchResultsTempTable (ROLENAME, DESCRIPTION, STATE)
SELECT AS_ROLENAME, AS_DESCRIPTION, AS_STATE
FROM AccountSpecification
-- always return the total number of items found in the search
SELECT @totalNumResults = @@ROWCOUNT
-- calculate the current page
DECLARE @nFirstPageRecord int
DECLARE @nLastPageRecord int
SELECT @nFirstPageRecord = (@nCurrentPage - 1) * @nPageSize
SELECT @nLastPageRecord = ((@nCurrentPage * @nPageSize) + 1)
-- select the correct page of data with the given page size
SELECT ROWID, ROLENAME, DESCRIPTION, STATE,
CASE
WHEN ROLENAME IN (select DISTINCT AS_ROLENAME from AccountItem) THEN 'false'
ELSE 'true'
END AS CanDelete FROM #SearchResultsTempTable
WHERE (ROWID > @nFirstPageRecord) AND (ROWID < @nLastPageRecord)GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
在C#程序中,通过:
pageCount = (int)System.Math.Ceiling((double)totalNumResults / pageSize);
来计算总页数,想跳到最后一页,则把pageCount的值作为@CurrentPage参数的输入变量即可。
select top 10 ...not in (select top 10*(当前页-1)....) group by ...
不过能否真的解决我现在的问题。。你是另一种思路来做的。
其实我觉的你写跟没有写到组件是一样的。你的意思组件是用其他语言写的吗?还是用dll把它封装,。。
如果写c#还不是写在cs里。
做了个function而已 :-)
能否给我一个公用的存储过程来实现分页例子
不好用吗?能不能说说说为什么?
如果真的不好,我就帮你想象这个问题.
MMKK说的是好方法,我们也在考虑作成这样。
我所介绍的这种分页方法还是比较好用的,我在好几个项目的开发中都采用这种技术实现分页。基本思路就是在数据库中创建一个带自增长ID字段的零时表,注意在创建零时表的时候在表名前加“#”,这样SQL Server在实际处理的时候并不会去创建一张真实的表,所以速度很快。应为在该零时表中有一个自增长ID字段,所以很容易就取出从n1~n2的记录,从而实现分页。
虽然你的表中记录随时有可能增加或着删除,这也没关系。因为在每次取一个分页的数据的时候,在存储过程中必然会执行一下语句:
CREATE TABLE #SearchResultsTempTable
(
ROWID int IDENTITY PRIMARY KEY,
ROLENAME varchar(100) NOT NULL ,
DESCRIPTION varchar(255) NULL,
STATE varchar(10)
)
这样就能保证“零时表”中数据和你真是表中数据同步。这种技术我最早是在微软的.net开发经典例子"PetShop"中看到的,刚开始我觉得这样反复创建零时表会影响系统性能,后来发现SQL Server在执行时根本不是真正去创建一个物理的表,因为SQL Server在执行时会进行算法优化。你可以先仿照我贴出来的存储过程实现自己的存储过程,有问题在联系。
这样在Web用户控件中就简单了一点,只需要处理各种按钮的事件,不必考虑数据处理了。而且省下了一半网络通讯时间(但是我觉得这个时间不重要,因为最多10条记录通讯量很小耶)。
感谢你的热情解答,你不能说是思路自相矛盾,这可是microsoft.com提供教程的一个分页的算法。我想把它完善一下。
一大堆的判断都写不出来。。不过吗最后一页的想法不错
5. 用户按键“最后一页”,很简单,Select top 10 from myTable order by {Keys} desc,结果中找出最后一条的主键,执行1.