续 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 续 (http://www.webasp.net/article/6/5196.htm) |
| -- 作者:未知 -- 发布日期: 2003-07-12 |
| 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" %> <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. |
| webasp.net |