码迷,mamicode.com
首页 > Web开发 > 详细

webform:分页组合查询

时间:2017-01-07 22:58:38      阅读:536      评论:0      收藏:0      [点我收藏+]

标签:dex   cmd   todo   实体   click   内容   文本   enabled   cli   

一个简单的分页组合查询页面

技术分享
  /// <summary>
    /// 查询方法
    /// </summary>
    /// <param name="tsql">SQL语句</param>
    /// <param name="hh">哈希表</param>
    /// <returns></returns>
    public List<Goods> Select(string uname, string sql, Hashtable hs)
    {
        List<Goods> glist = new List<Goods>();
        cmd.CommandText = sql;
        cmd.Parameters.Clear();
        foreach (string aa in hs.Keys)
        {
            cmd.Parameters.AddWithValue(aa, hs[aa]);
        }
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                Goods g = new Goods();
                g.Ids = Convert.ToInt32(dr["Ids"]);
                g.Sname = dr["Sname"].ToString();
                g.Number = dr["Number"].ToString();
                g.Date = Convert.ToDateTime(dr["Date"]);
                g.Shelf = Convert.ToInt32(dr["Shelf"]);
                g.Contacts = dr["Contacts"].ToString();
                g.Tel = dr["Tel"].ToString();
                g.UserName = uname;
                glist.Add(g);
            }
        }
        conn.Close();
        return glist;
    }

    /// <summary>
    /// 查询方法(页数)
    /// </summary>
    /// <param name="tsql"></param>
    /// <param name="hh"></param>
    /// <returns></returns>
    public List<Goods> Select(string uname, int count, int page, string sql, Hashtable hs)
    {
        List<Goods> glist = new List<Goods>();
        string sql1 = sql.Replace("*", "top " + count + " * ");
        string sql2 = sql.Replace("*", "top " + (page - 1) * count + " Ids ");
        cmd.CommandText = sql1 + " and Ids not in(" + sql2 + ")";
        cmd.Parameters.Clear();
        foreach (string aa in hs.Keys)
        {
            cmd.Parameters.AddWithValue(aa, hs[aa]);
        }
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                Goods g = new Goods();
                g.Ids = Convert.ToInt32(dr["Ids"]);
                g.Sname = dr["Sname"].ToString();
                g.Number = dr["Number"].ToString();
                g.Date = Convert.ToDateTime(dr["Date"]);
                g.Shelf = Convert.ToInt32(dr["Shelf"]);
                g.Contacts = dr["Contacts"].ToString();
                g.Tel = dr["Tel"].ToString();
                g.UserName = uname;
                glist.Add(g);
            }
        }
        conn.Close();
        return glist;

    }
数据访问类
技术分享
public class Goods
{
    public Goods()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public int Ids { get; set; }
    public string Sname { get; set; }
    public string Number { get; set; }
    public DateTime Date { get; set; }
    public int Shelf { get; set; }
    public string Contacts { get; set; }
    public string Tel { get; set; }
    public string UserName { get; set; }

    /// <summary>
    /// 判断有效期,以颜色区分
    /// </summary>
    public string color
    {
        get
        {
            string re = "";
            int a = Shelf - (DateTime.Now - Date).Days;
            if(a<=0)
                re = "background-color:#393535; color:red;";
            else if (a <= 7)
                re = "background-color:red;";
            else if (a <= 30&a>7)
                re = "background-color:yellow;";
            return re;
        }
    }
}
封装实体类
技术分享
public partial class Show : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["username"] != null)
            {
                string n = Request.Cookies["username"].Value;
                Label_NowPage.Text = "1";//第一页
                Label_MaxPage.Text = MaxPageNumber().ToString();//获取最大页
                btn_prev.Enabled = false;
                btn_first.Enabled = false;

                Repeater1.DataSource = new GoodsData().Select(Count, 1);
                Repeater1.DataBind();
                int max = MaxPageNumber();
                DropDownList1.Items.Clear();
                //给可快速跳转列表框赋值
                for (int i = 1; i <= max; i++)
                {
                    DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                string uname = new UsersData().Select(n).NickName;
                Literal1.Text = "欢迎你," + uname + "";
            }
            else
            {
                Response.Redirect("Login.aspx");
            }            

        }

        DropDownList1.SelectedIndexChanged += Button1_Click;
        btn_first.Click += btn_first_Click;//首页
        btn_prev.Click += btn_prev_Click;//上一页
        btn_next.Click += btn_next_Click;//下一页
        btn_last.Click += btn_last_Click;//末页
        Button1.Click += Button1_Click;//跳转按钮
        Button2.Click += Button2_Click;
        LinkButton1.Click += LinkButton1_Click;
    }

    string sql = "";
    //使用哈希表
    Hashtable hs = new Hashtable();
    private void groupselect()
    {
        sql = "select * from Warehouse";
        hs.Clear();
        //判断文本框中是否有内容需要查询
        if (TextBox1.Text.Trim().Length > 0)
        {
            //如果有内容,那么就拼接到Tsql语句中去
            sql += " where Sname like @a";
            hs.Add("@a", "%" + TextBox1.Text.Trim() + "%");
            hs.Add("@d", "%" + TextBox1.Text.Trim() + "%");
        }
        else
        {
            sql += " where 1=1";
        }
        if (TextBox2.Text.Trim().Length > 0)
        {
            sql += " and Shelf" + DropDownList2.SelectedValue + "@b";
            hs.Add("@b", TextBox2.Text.Trim());
            hs.Add("@e", TextBox2.Text.Trim());
        }
        else
        {
            sql += " and 1=1";
        }
        if (TextBox3.Text.Length > 0)
        {
            sql += " and Contacts like @c";
            hs.Add("@c", "%" + TextBox3.Text.Trim() + "%");
            hs.Add("@f", "%" + TextBox3.Text.Trim() + "%");
        }
        else
        {
            sql += " and 1=1";
        }
        Repeater1.DataSource = new GoodsData().Select(N, sql.Replace("*", "top " + Count + " *"), hs);
        Repeater1.DataBind();
    }
   
    void Button2_Click(object sender, EventArgs e)
    {
        groupselect();
        int max = MaxPageNumber();
        DropDownList1.Items.Clear();
        for (int i = 1; i <= max; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        Label_NowPage.Text = "1";
        Label_MaxPage.Text = MaxPageNumber().ToString();
        if (max == 1)
        {
            btn_first.Enabled = false;
            btn_prev.Enabled = false;
            btn_next.Enabled = false;
            btn_last.Enabled = false;
        }
        else
        {
            btn_first.Enabled = false;
            btn_prev.Enabled = false;
            btn_next.Enabled = true;
            btn_last.Enabled = true;
        }
    }

    void btn_prev_Click(object sender, EventArgs e)
    {
        groupselect();
        int max = MaxPageNumber();
        DropDownList1.Items.Clear();
        for (int i = 1; i <= max; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

        int nextPage = Convert.ToInt32(Label_NowPage.Text) - 1;

        Repeater1.DataSource = new GoodsData().Select(N, Count, nextPage, sql, hs);
        Repeater1.DataBind();

        Label_NowPage.Text = nextPage.ToString();
        panduan(nextPage);
    }

    void btn_first_Click(object sender, EventArgs e)
    {
        groupselect();
        int max = MaxPageNumber();
        DropDownList1.Items.Clear();
        for (int i = 1; i <= max; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        int nextPage = 1;

        Repeater1.DataSource = new GoodsData().Select(N,Count, nextPage, sql, hs);
        Repeater1.DataBind();

        Label_NowPage.Text = nextPage.ToString();
        panduan(nextPage);
    }

    void btn_next_Click(object sender, EventArgs e)
    {
        groupselect();
        int max = MaxPageNumber();
        DropDownList1.Items.Clear();
        for (int i = 1; i <= max; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        //获取当前页,计算要看的下一页的页号
        int nextPage = Convert.ToInt32(Label_NowPage.Text) + 1;
        //按照获取的页数绑定相应的数据
        Repeater1.DataSource = new GoodsData().Select(N,Count, nextPage, sql, hs);
        Repeater1.DataBind();
        //修改显示页数
        Label_NowPage.Text = nextPage.ToString();
        panduan(nextPage);     
    }

    void btn_last_Click(object sender, EventArgs e)
    {
        groupselect();
        int max = MaxPageNumber();
        DropDownList1.Items.Clear();
        for (int i = 1; i <= max; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        int nextPage = MaxPageNumber();
        //按照获取的页数绑定相应的数据
        Repeater1.DataSource = new GoodsData().Select(N, Count, nextPage, sql, hs);
        Repeater1.DataBind();

        //修改显示页数
        Label_NowPage.Text = nextPage.ToString();
        panduan(nextPage);

    }

    void Button1_Click(object sender, EventArgs e)
    {
        //获取当前页,计算要看的下一页的页号
        int nextPage = Convert.ToInt32(DropDownList1.SelectedValue);

        //按照获取的页数绑定相应的数据
        Repeater1.DataSource = new GoodsData().Select(Count, nextPage);
        Repeater1.DataBind();

        //修改显示页数
        Label_NowPage.Text = nextPage.ToString();

        if (nextPage <= 1)
        {
            btn_prev.Enabled = false;
        }
        btn_next.Enabled = true;
    }

    void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx");
    }
    string N = null;
    int Count = 6;
    public int MaxPageNumber()
    {
        //查询所有的数据
        List<Goods> glist = new List<Goods>();
        if (sql == "")
        {
            glist = new GoodsData().Select(N);
        }
        else
        {
            glist = new GoodsData().Select(N, sql, hs);
        }
        //取上限
        double end2 = Math.Ceiling(glist.Count / (Count * 1.0));

        int end = Convert.ToInt32(end2);

        return end;
    }
    public void panduan(int nextPage)
    {
        if (nextPage == 1)
        {
            btn_first.Enabled = false;
            btn_prev.Enabled = false;
            btn_next.Enabled = true;
            btn_last.Enabled = true;
        }
        else if (nextPage == MaxPageNumber())
        {
            btn_first.Enabled = true;
            btn_prev.Enabled = true;
            btn_next.Enabled = false;
            btn_last.Enabled = false;
        }
        else
        {
            btn_first.Enabled = true;
            btn_prev.Enabled = true;
            btn_next.Enabled = true;
            btn_last.Enabled = true;
        }
    }
}
展示界面后台代码

效果展示

1分页:技术分享

2单独条件查询技术分享

3多条件查询

 技术分享

4上下点击翻页(可直接点击下拉菜单进行跳转)

技术分享

 

webform:分页组合查询

标签:dex   cmd   todo   实体   click   内容   文本   enabled   cli   

原文地址:http://www.cnblogs.com/jiuban2391/p/6260436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!