Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->编程语言 ->ASP.NET ->正文

Simple Paging in Repeater and DataList Controls

来源:Linuxdby.com 作者:Webmaster 时间:2007-04-28 点击: [收藏] [投稿]
Simple Paging in Repeater and DataList Controls  
      
   

[Rate this Article]

Introduction
Most of the times while creating database driven web pages like product listing, employee directory, etc. we display the data in grid format. ASP.NET has come up with Web Controls like DataGrid, DataList and Repeater that allow you to display your data in tabular format easily.

Amongst the above three Web Controls the DataGrid Control is the most advanced and supports paging, templates etc. The other two controls are used in places where you need more control over the rendering of your data. The DataList and Repeater Controls have very flexible templates that give you total control over the formatting of your data, but one of the major drawbacks of these controls is that they do not support paging!!

Paging simply means splitting data display (UI) over number of pages in order to facilitate easy to browse interface and minimize the data sent out to the client.
For example, you have a web page displaying your products listing. It would not be a good idea to show all the 1000+ products you are offering on the one single page, since it will make the page difficult to browse as well as it will take a lot of time to display on the clients browser due to the size of data (plus a heavy load on your server).
The second reason this is not a good idea is that the client would not want to know about all the products you are selling since the last 10 years, he might visit to page to look out for the new products that you are offering.
In such scenarios, you divide the display of data into different pages and then display say 10 - 20 items per page and provide the client with links to view additional pages, this is called Paging.
Remember we are using server-side scripting so you don't have to physically create all the pages, you just have to code one page in such a way that it keeps paging all the records.

Hence some of the merits of paging are:
1) Easy to browse pages.
2) Faster to load pages on client side since the amount to display per page is less.
3) Less load on the database server, since for every user you only pull out a limited amount of data, rather than pulling out all the records for each client.

As I mentioned before, the DataList and Repeater controls are very flexible and there would be a large number of places you might want to use these controls. Even though these controls do not support Paging internally, in this article I will display a method using which, you can easily enable simple paging (previous , next ) in your DataList and Repeater controls.

25 October 2002, Update: The small error in the BuildPagers method has been corrected. Thanks to all readers who pointed out the bug!

Requirements
1) ASP.NET v1
2) SQL Server 7.0/2000/MSDE
(Optional, I am going to use the Northwind database that installs with the .NET SDK)

Simple Paging in Repeater Control

1) Listing 1, shows the code for a normal page that selects all records from the Products table and displays it using the Repeater control.


<%@ 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();
    myAdapter.Fill(ds,"Products");
    //DataBind the Repeater
    MyRepeater.DataSource = ds.Tables["Products"].DefaultView;
    MyRepeater.DataBind();
  }
</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">

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:ASP.NET编程中的十大技巧(建议进精华)   下一篇:用ASP.NET写你自己的代码生成器(1)。

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号