是一段分页的代码,我在程序中用了,可是只能显示前20个记录(分页的行数不同,显示的记录数也不同,我用的是4行分页)我看了半天也没看出毛病来,大虾们帮看看!
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %><script runat=server>
SqlConnection conNorthwind;
string  strSelect;
int intStartIndex;
int intEndIndex;void Page_Load(Object sender , EventArgs e) 
{
SqlCommand cmdSelect; conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
if (! IsPostBack ) 
{
// Get  Total Pages
strSelect = "Select Count(*) From Products";
cmdSelect = new SqlCommand( strSelect, conNorthwind );
conNorthwind.Open();
dgrdProducts.VirtualItemCount = ( (int)cmdSelect.ExecuteScalar() / dgrdProducts.PageSize );
conNorthwind.Close();
BindDataGrid();
}
}void BindDataGrid () 
{
SqlDataAdapter dadProducts;
DataSet dstProducts; intEndIndex = intStartIndex + dgrdProducts.PageSize;
strSelect = "Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order By ProductID";
dadProducts = new SqlDataAdapter( strSelect, conNorthwind );
dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex );
dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex );
dstProducts = new DataSet();
dadProducts.Fill( dstProducts ); dgrdProducts.DataSource = dstProducts;
dgrdProducts.DataBind();
}void dgrdProducts_PageIndexChanged( object s, DataGridPageChangedEventArgs e ) {
intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize );
dgrdProducts.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}</Script><html>
<head><title>DataGridCustomPaging.aspx</title></head>
<body>
<form Runat="Server"><asp:DataGrid
  ID="dgrdProducts"
  AllowPaging="True"
  AllowCustomPaging="True"
  PageSize="3"
  OnPageIndexChanged="dgrdProducts_PageIndexChanged"
  PagerStyle-Mode="NumericPages"
  CellPadding="3"
  Runat="Server" /></form>
</body>
</html>

解决方案 »

  1.   

    <%@ Page Language="C#" EnableViewState="True"%>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Data" %><script runat=server>
    SqlConnection conNorthwind;
    string  strSelect;
    int intStartIndex;
    int intEndIndex;void Page_Load(Object sender , EventArgs e) 
    {
    SqlCommand cmdSelect; conNorthwind = new SqlConnection("server=localhost;database=Northwind;uid=sa;password=;");
    if (! IsPostBack ) 
    {
    // Get  Total Pages
    strSelect = "Select Count(*) From Products";
    cmdSelect = new SqlCommand( strSelect, conNorthwind );
    conNorthwind.Open();
    dgrdProducts.VirtualItemCount = ( (int)cmdSelect.ExecuteScalar() / dgrdProducts.PageSize );
    conNorthwind.Close();
    BindDataGrid();
    }
    }void BindDataGrid () 
    {
    SqlDataAdapter dadProducts;
    DataSet dstProducts; intEndIndex = intStartIndex + dgrdProducts.PageSize;
    strSelect = "Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order By ProductID";
    dadProducts = new SqlDataAdapter( strSelect, conNorthwind );
    dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex );
    dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex );
    dstProducts = new DataSet();
    dadProducts.Fill( dstProducts ); dgrdProducts.DataSource = dstProducts;
    dgrdProducts.DataBind();
    }void dgrdProducts_PageIndexChanged( object s, DataGridPageChangedEventArgs e ) {
    intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize );
    dgrdProducts.CurrentPageIndex = e.NewPageIndex;
    BindDataGrid();
    }</Script><html>
    <head><title>DataGridCustomPaging.aspx</title></head>
    <body>
    <form Runat="Server" ID="Form1"><asp:DataGrid
      ID="dgrdProducts"
      AllowPaging="True"
      AllowCustomPaging="True"
      PageSize="4"
      OnPageIndexChanged="dgrdProducts_PageIndexChanged"
      PagerStyle-Mode="NumericPages"
      CellPadding="3"
      Runat="Server" /></form>
    </body>
    </html>
      

  2.   

    To: net_lover(孟子E章)
    首先你那颗大钻石我好羡慕啊(多少克拉的)
    请恕小弟眼拙,没能看出两个程序的差别啊!请明示!