- 热门文章:
- · Simple Paging in Repeater and DataList Controls
- · ASP.NET编程中的十大技巧(建议进精华)
- · 转贴:DataGrid/DataList
- · 用ASP.NET写你自己的代码生成器(1)。
- · ASP.NET中Cookie编程的基础知识(6)
- · ASP.NET中Cookie编程的基础知识(5)
- · ASP.NET中Cookie编程的基础知识(4)
- · ASP.NET中Cookie编程的基础知识(3)
- · ASP.NET中Cookie编程的基础知识(2)
- · ASP.NET中Cookie编程的基础知识(1)
- · .NET中窗体间相互访问的几种方式
- · .net中PictureBox中图片的拖动
上一篇:ms--help >>
续
public void BuildPagers()
{
//Check if its possible to have the previous page
if( ( int.Parse( CurrentPage.Value ) - 1 ) <= 0 )
{
Prev.Enabled = false;
}
else
{
Prev.Enabled = true ;
}
//Check if its possible to have the next page
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
>= int.Parse( TotalSize.Value ) )
{
Next.Enabled = false;
}
else
{
Next.Enabled = true ;
}
}
Listing 6 - BuildPagers method
The BuildPagers method shown in Listing 6, checks if its possible to show the respective LinkButtons and enables / disables them respectively. The logic of this method is very similar to the Page_Repeater method. One point worth nothing here is that this method is called after the Page_Repeater method is called, so that the value of the control with id CurrentPage has already been changed according to the button clicked. You can put a call to this method inside the BuildGrid method.
This completes our pager sample, save your page and test it out !! The complete code for the example is given in Listing 7.
<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load( Object sender , EventArgs e)
{
//Build the Grid only if the page has been accessed for the first time
if( !IsPostBack )
BuildGrid();
}
public void BuildGrid()
{
SqlConnection myConnection =
new SqlConnection(
"server=(local)\\NetSDK;database=Northwind;Trusted_Connection=yes" );
SqlDataAdapter myAdapter =
new SqlDataAdapter(
"SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products" ,
myConnection);
//Fill the DataSet
DataSet ds = new DataSet();
//Get the startRecord Count
//Remember database count is zero based so first decrease the value of
//the current page
int startRecord = ( int.Parse( CurrentPage.Value ) - 1 ) *
int.Parse( PageSize.Value );
//Fetch only the necessary records.
myAdapter.Fill( ds , startRecord , int.Parse( PageSize.Value ) ,
"Products");
//DataBind the Repeater
MyRepeater.DataSource = ds.Tables["Products"].DefaultView;
MyRepeater.DataBind();
//Second Part
//Create a new Command to select the total number of records
SqlCommand myCmd = new SqlCommand( "SELECT Count(*) from Products",
myConnection );
myConnection.Open();
//retrieve the value
TotalSize.Value = myCmd.ExecuteScalar().ToString() ;
myConnection.Close();
BuildPagers();
}
public void Page_Repeater( object sender, EventArgs e )
{
//Check for Button clicked
if( ((LinkButton)sender).ID == "Prev" )
{
//Check if we are on any page greater than 0
if( ( int.Parse( CurrentPage.Value ) - 1 ) >= 0 )
{
//Decrease the CurrentPage Value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) - 1 ).ToString() ;
}
}
else if( ((LinkButton)sender).ID == "Next" )
{
//Check if we can display the next page.
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
< int.Parse( TotalSize.Value ) )
{
//Increment the CurrentPage value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) + 1 ).ToString() ;
}
}
//Rebuild the Grid
BuildGrid();
}
public void BuildPagers()
{
//Check if its possible to have the previous page
if( ( int.Parse( CurrentPage.Value ) - 1 ) <= 0 )
{
Prev.Enabled = false;
}
else
{
Prev.Enabled = true ;
}
//Check if its possible to have the next page
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
>= int.Parse( TotalSize.Value ) )
{
Next.Enabled = false;
}
else
{
Next.Enabled = true ;
}
}
</script>
<body>
<h1>Products Listing</h1>
<form runat="server">
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<table width="100%" border="1" cellpadding="1" cellspacing="2">
<tr>
<th>
Product ID
</th>
<th>
Product
</th>
<th>
Quantity Per Unit
</th>
<th>
Unit Price
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "ProductID") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "QuantityPerUnit") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "$ {0}") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</ASP:Repeater>
<input type="hidden" id="PageSize" value="10" runat="server">
<input type="hidden" id="CurrentPage" value="1" runat="server">
<input type="hidden" id="TotalSize" runat="server">
<asp:LinkButton id="Prev" Text="<< Previous" OnClick="Page_Repeater" runat="server" />
<asp:LinkButton id="Next" Text="Next >>" OnClick="Page_Repeater" runat="server" />
</form>
</body>
</html>
Listing 7 - Simple Paging in Repeater (Full Code)
Simple Paging in DataList Control
The method that I have used above works the same way with DataList controls too, so I am not repeating the steps again. Instead I am including the full source code for an example that uses a DataList control to display the same data.
<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load( Object sender , EventArgs e)
{
//Build the Grid only if the page has been accessed for the first time
if( !IsPostBack )
BuildGrid();
}
public void BuildGrid()
{
SqlConnection myConnection =
new SqlConnection(
"server=(local)\\NetSDK;database=Northwind;Trusted_Connection=yes" );
SqlDataAdapter myAdapter =
new SqlDataAdapter(
"SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products" ,
myConnection);
//Fill the DataSet
DataSet ds = new DataSet();
//Get the startRecord Count
//Remember database count is zero based so first decrease the value of
//the current page
int startRecord = ( int.Parse( CurrentPage.Value ) - 1 ) *
int.Parse( PageSize.Value );
//Fetch only the necessary records.
myAdapter.Fill( ds , startRecord , int.Parse( PageSize.Value ) , "Products");
//DataBind the DataList
MyDataList.DataSource = ds.Tables["Products"].DefaultView;
MyDataList.DataBind();
//Second Part
//Create a new Command to select the total number of records
SqlCommand myCmd = new SqlCommand( "SELECT Count(*) from Products",
myConnection );
myConnection.Open();
//retrieve the value
TotalSize.Value = myCmd.ExecuteScalar().ToString() ;
myConnection.Close();
BuildPagers();
}
public void Page_DataList( object sender, EventArgs e )
{
//Check for Button clicked
if( ((LinkButton)sender).ID == "Prev" )
{
//Check if we are on any page greater than 0
if( ( int.Parse( CurrentPage.Value ) - 1 ) >= 0 )
{
//Decrease the CurrentPage Value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) - 1 ).ToString() ;
}
}
else if( ((LinkButton)sender).ID == "Next" )
{
//Check if we can display the next page.
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
< int.Parse( TotalSize.Value ) )
{
//Increment the CurrentPage value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) + 1 ).ToString() ;
}
}
//Rebuild the Grid
BuildGrid();
}
public void BuildPagers()
{
//Check if its possible to have the previous page
if( ( int.Parse( CurrentPage.Value ) - 1 ) <= 0 )
{
Prev.Enabled = false;
}
else
{
Prev.Enabled = true ;
}
//Check if its possible to have the next page
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
>= int.Parse( TotalSize.Value ) )
{
Next.Enabled = false;
}
else
{
Next.Enabled = true ;
}
}
</script>
<body>
<h1>Products Listing</h1>
<form runat="server">
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "ProductName") %></i></b>
</div>
<br>
<b>Product ID: </b><%# DataBinder.Eval(Container.DataItem, "ProductID") %><br>
<b>Quantity per Unit: </b>
<%# DataBinder.Eval(Container.DataItem, "QuantityPerUnit") %>
<br>
<b>Price: </b><%# DataBinder.Eval(Container.DataItem, "UnitPrice", "$ {0}") %><p>
</div>
</ItemTemplate>
</ASP:DataList>
<input type="hidden" id="PageSize" value="10" runat="server">
<input type="hidden" id="CurrentPage" value="1" runat="server">
<input type="hidden" id="TotalSize" runat="server">
<asp:LinkButton id="Prev" Text="<< Previous" OnClick="Page_DataList" runat="server" />
<asp:LinkButton id="Next" Text="Next >>" OnClick="Page_DataList" runat="server" />
</form>
</body>
</html>
Listing 7 - Simple Paging in DataList (Full Code)
Conclusion
In this article I displayed how easy it is to enable paging in Repeater and DataList controls. You can easily extend this sample to enable advanced paging with page numbers.
相关文章:
- · 在.NET上如何根据字符串动态创建控件
- · .NET 窗体之间的交互
- · 使用UltraWinGrid时双击的处理
- · .Net 下的Wondows窗体常用项目
- · 在.net中实现与ASP完全兼容的MD5算法(包括中文字符)
- · .Net FrameWork SDK文档的例子演示
- · 利用.NET语言开发自己的脚本语言(一)
- · .NET中的数据类型的一些变化
- · 网上发现的文章(测试驱动开发)
- · .NET程序实现多语言
- · .NET Framework中使用XML Web Service(2)
- · .NET Framework中使用XML Web Service(1)
- · 管理三元式的新思路,涉及到查询时似乎可以借用Social Network的思想
- · 使用AOP微型框架的例子
- · VB.NET中使用FTP下载文件的两种方法
- · .net下基于API封装的DirectUIHWND窗体访问
- · 在.net中调用存储过程的另一种方法
- · .NET Remoting 实现分布式数据库查询
- · 使用CodeDom开发基于B/S的.NET+MSSQL代码生成器的随感
- · 利用.NET的Reflection增强对象工厂的扩展性
- · .NET框架中基于角色的安全性(3)
- · .NET框架中基于角色的安全性(2)
- · .NET框架中基于角色的安全性(1)
- · .net的4个基本概念
- · 部署.net平台的程序
- · .net中何有效的使用Cache
- · 关于如何操作其他窗体的控件或变量的方法!
- · .net官方编码方法和命名规则
- · .NET 开发AutoCAD2006指南(二)
- · .NET开发AutoCAD指南(一)
- · 用.net 2003开发Windows CE应用,解决与pocket pc通讯的问题
- · .NET 开发AutoCAD2006指南(二)
- · 《.NET软件技术学习与实践》之序言
- · .net remoting范例
- · .net缓存应用与分析
- · 一种改进的轻量级.NET应用程序性能测试框架
- · 用.NET创建Windows服务
- · .net中清除EXCEL进程最有效的方法
