我是靠谱客的博主 知性乐曲,最近开发中收集的这篇文章主要介绍运用lambda表达式写分页加组合查、加各种限制,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

实体类、数据访问类:

public class car
{
   
    public string Code { get; set; }
    public string Name { get; set; }
    public decimal Oil { get; set; }
    public int Powers { get; set; }
    public int Exhaust { get; set; }
    public decimal Price { get; set; }
}
public class cardata
{
    SqlConnection conn = null;
    SqlCommand cmd = null;
    public cardata()
    {
        conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
        cmd = conn.CreateCommand();
    }
    public List<car> SelectAll()
    {
        List<car> clist = new List<car>();
        cmd.CommandText = "select *from car";
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            car c = new car();
            c.Code = dr["code"].ToString();
            c.Name = dr["name"].ToString();
            c.Oil = Convert.ToDecimal(dr["oil"]);
            c.Powers = Convert.ToInt32(dr["powers"]);
            c.Exhaust = Convert.ToInt32(dr["exhaust"]);
            c.Price = Convert.ToDecimal(dr["price"]);
            clist.Add(c);
        }
        conn.Close();
        return clist;
    }
}

前端aspx:

<body>
    <form id="form1" runat="server">

     车名:   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="查询" /><br /><br />

    <table style="width: 100%; text-align: center; background-color: navy;">
            <tr style="color: white;">
      
                <td>编号</td>
                <td>名称</td>
                <td>油耗</td>
                <td>马力</td>
                <td>排量</td>
                <td>价格</td>
            </tr>
            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <tr style="background-color: white;">
                   
                        <td><%#Eval("Code") %></td>
                        <td><%#Eval("Name") %></td>
                        <td><%#Eval("Oil") %></td>
                        <td><%#Eval("Powers") %></td>
                        <td><%#Eval("Exhaust") %></td>
                        <td><%#Eval("Price") %></td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>


        当前第[
       <asp:Label ID="Label_now" runat="server" Text="1"></asp:Label>
        ]页&nbsp;&nbsp;
        共[
        <asp:Label ID="Label_max" runat="server" Text=""></asp:Label>
        ]页&nbsp;&nbsp;
        <asp:Button ID="btn_first" runat="server" Text="首页" />
        <asp:Button ID="btn_prev" runat="server" Text="上一页" />
        <asp:Button ID="btn_next" runat="server" Text="下一页" />
        <asp:Button ID="btn_last" runat="server" Text="尾页" />
        &nbsp;&nbsp;
        <asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server"></asp:DropDownList>
        <asp:Button ID="btn_jump" runat="server" Text="跳转" />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>



    </form>
</body>

后台:

public partial class _Default : System.Web.UI.Page
{
    int count = 5;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater1.DataSource = new cardata().SelectAll().Take(count);
            Repeater1.DataBind();

            Label_max.Text = max().ToString();

            btn_prev.Enabled = false;

            DropDownList1.Items.Clear();
            for (int i = 1; i <= Convert.ToInt32(Label_max.Text); i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                DropDownList1.Items.Add(li);
            }
        }
       
               
        btn_prev.Click += btn_prev_Click;
        btn_next.Click += btn_next_Click;
        Button1.Click += Button1_Click;
        btn_first.Click += btn_first_Click;
        btn_last.Click += btn_last_Click;
        btn_jump.Click += btn_jump_Click;
    }

    void btn_jump_Click(object sender, EventArgs e)
    {
        Label_now.Text = DropDownList1.SelectedValue;
        select(Convert.ToInt32(Label_now.Text));
        if (Label_now.Text == "1")
        {
            btn_next.Enabled = true;
            btn_prev.Enabled = false;
        }
        else if (Label_now.Text == max().ToString())
        {
            btn_next.Enabled = false;
            btn_prev.Enabled = true;
        }
        else
        {
            btn_next.Enabled = true;
            btn_prev.Enabled = true;
        }
    }

    void btn_last_Click(object sender, EventArgs e)
    {
        int nowpage = max();
        select(nowpage);
        Label_now.Text = nowpage.ToString();

        btn_next.Enabled = false;
        btn_prev.Enabled = true;
    }

    void btn_first_Click(object sender, EventArgs e)
    {
        int nowpage = 1;
        select(nowpage);
        Label_now.Text = nowpage.ToString();

        btn_next.Enabled = true;
        btn_prev.Enabled = false;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        int nowpage = 1;
        select(nowpage);
        Label_now.Text = nowpage.ToString();
        Label_max.Text = max().ToString();

        btn_prev.Enabled = false;
        if (Label_now.Text == Label_max.Text)
        { btn_next.Enabled = false; }
        else
        { btn_next.Enabled = true; }

        DropDownList1.Items.Clear();
        for (int i = 1; i <= Convert.ToInt32(Label_max.Text); i++)
        {
            ListItem li = new ListItem();
            li.Value = i.ToString();
            li.Text = i.ToString();
            DropDownList1.Items.Add(li);
        }
    }

    void btn_next_Click(object sender, EventArgs e)
    {
        int nowpage = Convert.ToInt32(Label_now.Text) + 1;
        select(nowpage);
        Label_now.Text = nowpage.ToString();
        if (Convert.ToInt32(Label_now.Text) >= max())
        {
            btn_next.Enabled = false;
        }
        btn_prev.Enabled = true;
    }

    void btn_prev_Click(object sender, EventArgs e)
    {
        int nowpage = Convert.ToInt32(Label_now.Text) - 1;
        select(nowpage);
        Label_now.Text = nowpage.ToString();
        if (Convert.ToInt32(Label_now.Text) <= 1)
        {
            btn_prev.Enabled = false;
        }
        btn_next.Enabled = true;
    }



    public void select(int nowpage)
    {
        List<car> clist = new cardata().SelectAll().Where(r => r.Name.Contains(TextBox1.Text)).ToList();
        //clist = clist.Where(r=>r.Price>50 &&r.Price<60||r.Price<30).ToList();
        clist = clist.Skip((nowpage - 1) * count).Take(count).ToList();
        Repeater1.DataSource = clist;
        Repeater1.DataBind();

    }

    public int max()
    {
        List<car> clist= new cardata().SelectAll().Where(r => r.Name.Contains(TextBox1.Text)).ToList();
        //clist = clist.Where(r => r.Price > 50 && r.Price < 60 || r.Price < 30).ToList();
        int allcount = clist.Count;
        int allnum = Convert.ToInt32(Math.Ceiling(allcount * 1.0 / count * 1.0));
        return allnum;
    }




}

 

转载于:https://www.cnblogs.com/wy1992/p/6904373.html

最后

以上就是知性乐曲为你收集整理的运用lambda表达式写分页加组合查、加各种限制的全部内容,希望文章能够帮你解决运用lambda表达式写分页加组合查、加各种限制所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部