在云那方

首页 新随笔 联系 订阅 管理

DLL下载:/Files/prolifes/aspnetpager.rar

不同版本下载地址:http://www.webdiyer.com/download/default.aspx

1.Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">

</head>
<body>
    
<form id="form1" runat="server">
        
<div align="center">
        
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
        
</div>
            
<div align="center">
                
<table>
                    
<tr>
                        
<td><%=AspNetPager1.RecordCount%>条记录,每页显示<%=PageSize%></td>
                        
<td><webdiyer:AspNetPager id="AspNetPager1" runat="server" AlwaysShow="True" OnPageChanged="AspNetPager1_PageChanged"></webdiyer:AspNetPager></td>
                    
</tr>
                
</table>
            
</div>
    
</form>
</body>
</html>

2.Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    
public int PageSize = 10;
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            AspNetPager1.PageSize 
= PageSize;//设置每页显示数
            AspNetPager1.RecordCount = GetCount();//得到总记录数
            GridViewBinder(AspNetPager1.CurrentPageIndex);
        }
    }

    
/// <summary>
    
/// 得到记录总数
    
/// </summary>
    
/// <returns></returns>
    private int GetCount()
    {
        
string sql = "select count(*) from Orders";
        DataSet dt 
= FindMange(sql);
        
return int.Parse(dt.Tables[0].Rows[0][0].ToString());
    }

    
/// <summary>
    
/// 绑定
    
/// </summary>
    
/// <param name="Page"></param>
    private void GridViewBinder(int Page)
    {
        
string sql = "select top " + PageSize + " * from Orders where OrderId not in(select top " + (Page - 1* PageSize + " OrderId from Orders order by OrderId) order by OrderId";
        DataSet dt 
= FindMange(sql);

        GridView1.DataSource 
= dt.Tables[0];
        GridView1.DataBind();
    }

    
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        GridViewBinder(AspNetPager1.CurrentPageIndex);
    }

    
private DataSet FindMange(string sql)
    {
        
string conSQL = "server=.;uid=sa;pwd=123;database=northwind";
        SqlConnection objConn 
= new SqlConnection(conSQL);
        
try
        {
            objConn.Open();
            DataSet dt 
= new DataSet();
            SqlDataAdapter da 
= new SqlDataAdapter(sql, objConn);
            da.Fill(dt);
            objConn.Close();
            objConn.Dispose();
            
return dt;
        }
        
catch (SqlException e)
        {
            
throw e;
        }
    }
}
posted on 2008-07-04 14:50  Rich.T  阅读(360)  评论(0编辑  收藏  举报