Linq 组合分页查询

时间:2021-12-27 23:21:43

  实现功能:

    组合查询筛选出来的条件,实现分页功能,上一页,下一页,首页,尾页,页面跳转

页面效果:

  

Linq 组合分页查询

页面代码:

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

<!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">
        <br /><br />
        用户名:<asp:TextBox ID="Text_uname" runat="server"></asp:TextBox>
        成绩:<asp:DropDownList ID="Dr_Sco" runat="server">
            <asp:ListItem Value="null" Text="全部"></asp:ListItem>
            <asp:ListItem Value="0,60" Text="小于60"></asp:ListItem>
            <asp:ListItem Value="60,70 " Text="60至70"></asp:ListItem>
            <asp:ListItem Value="70,80" Text="70至80"></asp:ListItem>
            <asp:ListItem Value="80,100" Text="80至100"></asp:ListItem>
           </asp:DropDownList>
        班级:<asp:DropDownList ID="Dr_Cla" runat="server">
            <asp:ListItem Text="全部" Value="null"></asp:ListItem>
            <asp:ListItem Text="C001" Value="C001"></asp:ListItem>
            <asp:ListItem Text="C002" Value="C002"></asp:ListItem>
            <asp:ListItem Text="C003" Value="C003"></asp:ListItem>
            <asp:ListItem Text="C004" Value="C004"></asp:ListItem>
           </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="搜索" />

        <br /><br />
        <table style="text-align: center; background-color: red; color: black;width:100%;">
            <tr>
                <td>Ids</td>
                <td>姓名</td>
                <td>密码</td>
                <td>标题</td>
                <td>成绩</td>
                <td>班级</td>
            </tr>

            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <tr style="background-color:white;color:black;">
                        <td><%#Eval("Ids") %></td>
                        <td><%#Eval("UserName") %></td>
                        <td><%#Eval("PassWord") %></td>
                        <td><%#Eval("Title") %></td>
                        <td><%#Eval("Score") %></td>
                        <td><%#Eval("Class") %></td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
        <br /><br />
        <div>
            
            当前【<asp:Label ID="Lab_nowpage" runat="server" Text="1"></asp:Label>】页,
            共【<asp:Label ID="Lab_sunpage" runat="server" Text=""></asp:Label>】页;

            <asp:Button ID="But_first" runat="server" Text="首页" />

            <asp:Button ID="But_up" runat="server" Text="上一页"  OnClientClick="return go1()"/>

            <asp:Button ID="But_next" runat="server" Text="下一页" OnClientClick="return go()" />

            <asp:DropDownList ID="Dr_ye" runat="server"></asp:DropDownList>
            <asp:Button ID="But_drow" runat="server" Text="跳转" />

            <asp:Button ID="But_last" runat="server" Text="尾页" />
        </div>


    </form>
</body>
</html>
<script type="text/javascript">
    function go() {
        var has = true;
        var a = document.getElementById('Lab_nowpage').innerText;
        
        var b = document.getElementById('Lab_sunpage').innerText;
        if (a == b) {
            return false;
        }
    }

    function go1() {
        var has = true;
        var a = document.getElementById('Lab_nowpage').innerText;
        
        var b = document.getElementById('Lab_sunpage').innerText;
        if (a <2) {
            return false;
        }
    }

</script>

后台代码:

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

public partial class _Default : System.Web.UI.Page
{
    int pagecount = 5;
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
        But_next.Click += But_next_Click;
        But_up.Click += But_up_Click;
        But_first.Click += But_first_Click;
        But_last.Click += But_last_Click;
        But_drow.Click += But_drow_Click;



        if (!IsPostBack)
        {
            using (stuDataClassesDataContext con = new stuDataClassesDataContext())
            {
                Repeater1.DataSource = Data().Skip(0).Take(pagecount);
                Repeater1.DataBind();

                
                for (int i = 1; i <= Convert.ToInt32(Lab_sunpage.Text);i++ )
                {
                    ListItem li = new ListItem(i.ToString(),i.ToString());
                    Dr_ye.Items.Add(li);
                }
                //Dr_ye.Items.FindByText("2").Selected = true;
            }
        }

    }
    //跳转页面
    void But_drow_Click(object sender, EventArgs e)
    {
        int aa = Convert.ToInt32( Dr_ye.SelectedValue);
        Repeater1.DataSource = Data().Skip((aa - 1) * 5).Take(pagecount);
        Repeater1.DataBind();
        Lab_nowpage.Text = aa.ToString();
    }
    //最后一页
    void But_last_Click(object sender, EventArgs e)
    {
        Repeater1.DataSource = Data().Skip((Convert.ToInt32( Lab_sunpage.Text)-1)*5).Take(pagecount);
        Repeater1.DataBind();
        Lab_nowpage.Text = Lab_sunpage.Text;
    }
    //第一页
    void But_first_Click(object sender, EventArgs e)
    {
        Repeater1.DataSource = Data().Skip(0).Take(pagecount);
        Repeater1.DataBind();
        Lab_nowpage.Text = "1";
    }

    //上一页的事件
    void But_up_Click(object sender, EventArgs e)
    {
        int aaa = Convert.ToInt32(Lab_nowpage.Text) - 1;
        Repeater1.DataSource = Data().Skip((aaa - 1) * 5).Take(pagecount);
        Repeater1.DataBind();
        Lab_nowpage.Text = aaa.ToString();

        Dr_ye.ClearSelection();
        for (int i = 0; i < Dr_ye.Items.Count; i++)
        {
            if (Dr_ye.Items[i].Text == Lab_nowpage.Text)
            {
                Dr_ye.Items[i].Selected = true;
            }
        }
    }
    //下一页的事件
    void But_next_Click(object sender, EventArgs e)
    {

        int aaa = Convert.ToInt32(Lab_nowpage.Text) + 1;
        Repeater1.DataSource = Data().Skip((aaa - 1) * pagecount).Take(pagecount);
        Repeater1.DataBind();
        Lab_nowpage.Text = aaa.ToString();

        //清空之前的选中项
        Dr_ye.ClearSelection();
        for (int i = 0; i < Dr_ye.Items.Count; i++)
        {
            if (Dr_ye.Items[i].Text == Lab_nowpage.Text)
            {
                Dr_ye.Items[i].Selected = true;
            }
        }



    }
    //搜索按钮事件
    void Button1_Click(object sender, EventArgs e)
    {
        Lab_nowpage.Text = "1";
        Repeater1.DataSource = Data().Skip(0).Take(pagecount);
        Repeater1.DataBind();
        Lab_sunpage.Text = Lab_sunpage.Text;
        Dr_ye.Items.Clear();
        for (int i = 1; i <= Convert.ToInt32(Lab_sunpage.Text); i++)
        {
            ListItem li = new ListItem(i.ToString(), i.ToString());
            Dr_ye.Items.Add(li);
        }

    }
//查询数据的方法
    public List<ab> Data()
    {
        using (stuDataClassesDataContext con = new stuDataClassesDataContext())
        {
            List<ab> alist = con.ab.ToList();
            if (Text_uname.Text.Trim().Length > 0)
            {
                alist = alist.Where(r => r.UserName.Contains(Text_uname.Text)).ToList();
            }

            if(Dr_Sco.SelectedValue!="null")
            {
                string score = Dr_Sco.SelectedValue;
                string[] ab = score.Split(',');
                alist = alist.Where(r => Convert.ToInt32(r.Score) >= Convert.ToInt32(ab[0]) && Convert.ToInt32(r.Score) < Convert.ToInt32(ab[1])).ToList();
            }
            if (Dr_Cla.SelectedValue != "null")
            {
                alist = alist.Where(r => r.Class == Dr_Cla.SelectedValue).ToList();
            }

            int endcount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(alist.Count) / pagecount));
            Lab_sunpage.Text = endcount.ToString();


            return alist;
        }

    }
}