繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> 续

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:17  文字大小:【】【】【
简介:5) Lastly, we need a method that enables and disables the LinkButtons depending on the number of records available, i.e. if you are on the first page, there is no use of displaying the link to go ...

5) Lastly, we need a method that enables and disables the LinkButtons depending on the number of records available, i.e. if you are on the first page, there is no use of displaying the link to go to the previous page - right ??

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" %>

Products Listing

Product ID

Product

Quantity Per Unit

Unit Price

<%# DataBinder.Eval(Container.DataItem, "ProductID") %>

<%# DataBinder.Eval(Container.DataItem, "ProductName") %>

<%# DataBinder.Eval(Container.DataItem, "QuantityPerUnit") %>

<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "$ {0}") %>

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" %>

Products Listing

RepeatDirection="Horizontal" runat="server">

<%# DataBinder.Eval(Container.DataItem, "ProductName") %>


Product ID: <%# DataBinder.Eval(Container.DataItem, "ProductID") %>

Quantity per Unit:

<%# DataBinder.Eval(Container.DataItem, "QuantityPerUnit") %>


Price: <%# DataBinder.Eval(Container.DataItem, "UnitPrice", "$ {0}") %>

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. <

责任编辑:admin
相关文章