WebForm分页浏览

Posted 王炜忠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebForm分页浏览相关的知识,希望对你有一定的参考价值。

1.封装类
//封装类
using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Users 的摘要说明
/// </summary>
public class Users
{

    private string _UserName;

    public string UserName
    {
        get { return _UserName; }
        set { _UserName = value; }
    }
    private string _Password;

    public string Password
    {
        get { return _Password; }
        set { _Password = value; }
    }
    private string _NickName;

    public string NickName
    {
        get { return _NickName; }
        set { _NickName = value; }
    }
    private bool _Sex;

    public bool Sex
    {
        get { return _Sex; }
        set { _Sex = value; }
    }
    public string SecStr
    {
        get { return _Sex ? "" : ""; }
    }
    private DateTime _Birthday;

    public DateTime Birthday
    {
        get { return _Birthday; }
        set { _Birthday = value; }
    }
    public string BirthdayStr
    {
        get { return _Birthday.ToString("yyyy年MM月dd日"); }
    }
    private string _Nation;

    public string Nation
    {
        get { return _Nation; }
        set { _Nation = value; }
    }


}
View Code

2连接数据库,建立方法

using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// UserData 的摘要说明
/// </summary>
public class UserData
{
    SqlConnection cnn = null;
    SqlCommand cmd = null;

    public UserData()
    {
        cnn = new SqlConnection("server=.;database=ren;User=sa;pwd=123");
        cmd = cnn.CreateCommand();
    }
    /// <summary>
    /// 查询数据库所有内容
    /// </summary>
    /// <returns>泛型集合</returns>
    public List<Users> quan()
    {
        List<Users> ulist = new List<Users>();
        cmd.CommandText = "select * from Users";
        cnn.Open();
        SqlDataReader ss = cmd.ExecuteReader();
        while(ss.Read())
        {
            Users u = new Users();
            u.UserName = ss[0].ToString();
            u.Password = ss[1].ToString();
            u.NickName = ss[2].ToString();
            u.Sex = Convert.ToBoolean(ss[3]);
            u.Birthday = Convert.ToDateTime(ss[4]);
            u.Nation = ss[5].ToString();
            ulist.Add(u);
        }
        cnn.Close();
        return ulist;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="dijiye">第几页</param>
    /// <param name="perpagenum">每一页的数量</param>
    /// <returns></returns>
    public List<Users> select(int dijiye,int perpagenum)
    {
        List<Users> ulist = null;
        cmd.CommandText = " select top " + perpagenum + " * from Users where UserName not in(select top " + (perpagenum*(dijiye - 1)) + " UserName from Users)";
        cnn.Open();
        SqlDataReader ss = cmd.ExecuteReader();
        if (ss.HasRows)
        {
            ulist = new List<Users>();
            while (ss.Read())
            {
                Users u = new Users();
                u.UserName = ss[0].ToString();
                u.Password = ss[1].ToString();
                u.NickName = ss[2].ToString();
                u.Sex = Convert.ToBoolean(ss[3]);
                u.Birthday = Convert.ToDateTime(ss[4]);
                u.Nation = ss[5].ToString();
                ulist.Add(u);
            }
        }
        cnn.Close();
        return ulist;
    }
    /// <summary>
    /// 根据SQl语句,查询符合条件的数据
    /// </summary>
    /// <param name="sql">SQl查询条件</param>
    /// <returns></returns>
    public List<Users> zuhe(string sql)
    {
        List<Users> ulist = new List<Users>();
        cmd.CommandText =sql;
        cnn.Open();
        SqlDataReader ss = cmd.ExecuteReader();
        while (ss.Read())
        {
            Users u = new Users();
            u.UserName = ss[0].ToString();
            u.Password = ss[1].ToString();
            u.NickName = ss[2].ToString();
            u.Sex = Convert.ToBoolean(ss[3]);
            u.Birthday = Convert.ToDateTime(ss[4]);
            u.Nation = ss[5].ToString();
            ulist.Add(u);
        }
        cnn.Close();
        return ulist;
    }





}
View Code

3.页面设置

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table style="background-color:blue; width:100%">
                    <tr style="background-color:yellow;">
                        <td>姓名</td>
                        <td>密码</td>
                        <td>昵称</td>
                        <td>性别</td>
                        <td>生日</td>
                        <td>民族</td>
                    </tr>

            </HeaderTemplate>
            <ItemTemplate>
                <tr style="background-color:gray;">
                        <td><%#Eval("Username") %></td>
                        <td><%#Eval("Password") %></td>
                        <td><%#Eval("NickName") %></td>
                        <td><%#Eval("Sex") %></td>
                        <td><%#Eval("Birthday") %></td>
                        <td><%#Eval("Nation") %></td>
                </tr>


            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
           

        </asp:Repeater>
         当前第<asp:Label ID="dijiye" runat="server" Text="Label"></asp:Label>页,共<asp:Label ID="gongjiye" runat="server" Text="Label"></asp:Label>页  <asp:LinkButton ID="shangyiye" runat="server">上一页</asp:LinkButton>
        <asp:LinkButton ID="xiayiye_button" runat="server">下一页</asp:LinkButton>&nbsp&nbsp跳转到<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server"></asp:DropDownList></form>
</body>
</html>
View Code

4、设置事件

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default1 : System.Web.UI.Page
{
    int pagecount = 3;//规定每页的数量
    protected void Page_Load(object sender, EventArgs e)
    {
        gongjiye.Text = maxpage().ToString();
        //gongjiye.Text = new UserData().select(1, pagecount).Count.ToString();
        shangyiye.Click += shangyiye_Click;//上一页的点击事件
        xiayiye_button.Click += xiayiye_button_Click;//下一页点击事件
        DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;//索引变化事件
        if (!IsPostBack)
        {
            Repeater1.DataSource = new UserData().select(1,pagecount);//第一页显示
            Repeater1.DataBind();
            dijiye.Text = "1";//页面开始运行就是第一页

            for (int i = 1; i <= maxpage(); i++)
            {
                DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));//跳转按钮DropDownList1的页数添加
            }

        }
    }

    void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//索引变化事件
    {
        int n = int.Parse(DropDownList1.SelectedValue);//取出选中哪一页
        Repeater1.DataSource = new UserData().select(n,pagecount);//显示所选页数的内容
        Repeater1.DataBind();//数据绑定
        dijiye.Text = n.ToString();
    }

    void xiayiye_button_Click(object sender, EventArgs e)//下一页点击事件
    {
        int xia = int.Parse(dijiye.Text) + 1;
        if (xia > maxpage())//下一页如果大于最大页
        {
            return;
        }
        Repeater1.DataSource = new UserData().select(xia,pagecount);
        Repeater1.DataBind();
        dijiye.Text = xia.ToString();


    }

    void shangyiye_Click(object sender, EventArgs e)//上一页的点击事件
    {
        int shang = int.Parse(dijiye.Text)-1;
        if (shang == 0)
        {
            return;
        }
        Repeater1.DataSource = new UserData().select(shang, pagecount);
        Repeater1.DataBind();
        dijiye.Text = shang.ToString();


    }

    public int maxpage()//求最多有多少页
    {
        int i;
        List<Users> ulist = new UserData().quan();
        if (ulist == null)
        {
            i = 0;
        }
        else
        {
            i = int.Parse(Math.Ceiling(ulist.Count/(pagecount*1.0)).ToString());
        }
        return i;
    }


}
View Code

总之:就是拼Sql查询语句:

         每页5行数据

          select top 5  * from Users where UserName not in(select top 0 UserName from Users);显示第一页信息

          select top 5  * from Users where UserName not in(select top 5 UserName from Users);显示第二页信息

          以此类推。。。。。。。

          select top " + perpagenum + " * from Users where UserName not in(select top " + (perpagenum*(dijiye - 1)) + " UserName from Users)

 

 

完!!!

 

.

以上是关于WebForm分页浏览的主要内容,如果未能解决你的问题,请参考以下文章

webform分页

C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段

C#-WebForm-★★★LinQ-数据的条件组合查询并进行分页展示(未加各种限定)★★★

webform:分页组合查询

webform组合查询和分页

Webform(分页与组合查询配合使用)