数据分页,just YY(未完,待完善)

数据库:db
表:create table fy(
       id int primary key auto_inrement,
       title varchar(50)
);

后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected string currentPage;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form["select"] == null)
        {
            currentPage = "1";
            return;
        }
        
        currentPage = Request.Form["select"];
        
        
    }
    //select top 20 * from fy as ty where id not in (select top 10 id from fy);
    protected StringBuilder sb(string num)
    {
        StringBuilder stbu = new StringBuilder();
        string str = "";
        int num2 = Convert.ToInt32(num) * 10;
        int num1 = num2 - 10;
        SqlConnection conn = new SqlConnection("server=.; database=db; integrated security=true;");
        conn.Open();
        SqlCommand cmd = new SqlCommand("select top " + num2 + " * from fy as ty where id not in (select top " + num1 + " id from fy)", conn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            str = sdr.GetString(sdr.GetOrdinal("title")).ToString() + "<br /><hr />";
            stbu.Append(str);
        }
        sdr.Close();
        cmd.Dispose();
        conn.Close();
        conn.Dispose();
        return stbu;
    }

    //获取 分页 的 数量
    protected int newsCount()
    {
        int n = 0;
        SqlConnection con = CreateConnection();
        con.Open();
        SqlCommand cmd = new SqlCommand("select Count(*) as num from fy", con);
        SqlDataReader sdr = cmd.ExecuteReader();
        if (sdr.Read())
        {
            n = sdr.GetInt32(sdr.GetOrdinal("num"));
        }
        if (n % 10 == 0)
        {
            n = n / 10;
        }
        if (n % 10 != 0)
        {
            n = n / 10 + 1;
        }
        sdr.Close();
        cmd.Dispose();
        con.Close();
        con.Dispose();
        return n;
    }
    protected StringBuilder CreateOption()
    {
        StringBuilder sb = new StringBuilder();
        int getCount = newsCount();
        for (int i = 1; i <= getCount; i++)
        {
            sb.Append("<option value='"+i.ToString()+"'>"+i.ToString()+"</option>");
        }
        return sb;
    }
    protected SqlConnection CreateConnection()
    {
        SqlConnection conn = new SqlConnection("server=.; database=db; integrated security=true;");
        return conn;
    }
}




<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
    <script type="text/javascript">
        function abc() {
           // alert("Mu JJ");
            var getSelect = document.getElementsByTagName("select")[0];
            getSelect.onchange = function() {
                document.getElementsByTagName("form")[0].submit();
            }
        }
    </script>
</head>
<body onload="abc()">
    <form id="form1" runat="server">
    <div>
        第<select name="select">
           <%=CreateOption()%>
        </select>页<br />
       
       
        <%=sb(currentPage) %>
        <%=newsCount()%>
    </div>
    </form>
</body>
</html>

posted @ 2011-05-21 16:53  sirzxj  阅读(165)  评论(0编辑  收藏  举报