首页 > 代码库 > 分页功能 与 分类查询功能合并

分页功能 与 分类查询功能合并

功能的合并  首要的就是要找出两个功能的共同点。这里主要是用Response展示数据,通过tsql语句查询的集合作为数据源,所以这里将两个tsql合并,来实现功能的合并。

面板界面代码:

<form id="form1" runat="server">
        <div>
            编号:<asp:TextBox ID="TextBox1" runat="server" onkeypress="if(event.keyCode<48||event.keyCode>57)event.returnValue=http://www.mamicode.com/false;" MaxLength="11"></asp:TextBox>
            名称:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            价格:<asp:DropDownList ID="DropDownList2" runat="server">
                <asp:ListItem Text="大于" Value=http://www.mamicode.com/">"></asp:ListItem>
                <asp:ListItem Text="小于" Value=http://www.mamicode.com/"<"></asp:ListItem>
                <asp:ListItem Text="大于等于" Value=http://www.mamicode.com/">="></asp:ListItem>
                <asp:ListItem Text="小于等于" Value=http://www.mamicode.com/"<="></asp:ListItem>
                <asp:ListItem Text="等于" Value=http://www.mamicode.com/"="></asp:ListItem>
               </asp:DropDownList>&nbsp;<asp:TextBox ID="TextBox3" runat="server" onkeypress="if(event.keyCode<48||event.keyCode>57)event.returnValue=http://www.mamicode.com/false;" MaxLength="11"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="查询" /><br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        <div>
            <table style="background-color:pink;width:100%;text-align:center">
                <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("ids") %></td>
                            <td><%#Eval("name") %></td>
                            <td><%#Eval("lprice") %></td>
                            <td><%#Eval("xprice") %></td>
                            <td><%#Eval("tu") %></td>
                            <td><%#Eval("xiangqing") %></td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
            <br />
            
            目前第【<asp:Literal ID="yeshu_1"  runat="server" Text="1"></asp:Literal>】页,共【<asp:Literal ID="yeshu_2" runat="server"></asp:Literal>】页。
            <asp:Button ID="but_1" runat="server" Text="第一页" /><asp:Button ID="but_s" runat="server" Text="上一页" /><asp:Button ID="but_x" runat="server" Text="下一页" /><asp:Button ID="but_0" runat="server" Text="最后一页" />
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
                <asp:ListItem Text="请选择" Value=http://www.mamicode.com/""></asp:ListItem>
            </asp:DropDownList>
            <asp:HiddenField ID="HF1" runat="server" />
            <asp:HiddenField ID="HF2" runat="server" />
        </div>
    </form>

 

捣鼓出来大体这样

 

技术分享

后台代码

public partial class _Default : System.Web.UI.Page
{
    int yehascount = 2;   //每页显示几条数据
    int tiaoshu = 0;   //总共有多少条符合条件的数据
    int yecount = 0;   //  总共有几页
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)//   只是在页面第一次刷新的时候加载这个
        {
            int a = new taobaodata().selectcount(count());
            List<taobao> tlist = tiaojian();
            Repeater1.DataSource = tlist;
            Repeater1.DataBind();
            tiaoshu = a;

            yeshu_2.Text = suan().ToString();
            if (Convert.ToInt32(yeshu_1.Text) <= 1)
            {
                but_1.Visible = false;//让按钮不可见
                but_s.Visible = false;
                but_x.Visible = true;
                but_0.Visible = true;

            }
            else if (Convert.ToInt32(yeshu_1.Text) >= Convert.ToInt32(yeshu_2.Text))
            {
                but_x.Visible = false;
                but_0.Visible = false;
                but_1.Visible = true;
                but_s.Visible = true;
            }

            for (int i = 1; i <= Convert.ToInt32(yeshu_2.Text); i++)
            {
                ListItem li = new ListItem("" + i.ToString() + "", i.ToString());

                DropDownList1.Items.Add(li);/////下面的那个dropdownlist
            }

        }
        //Label1.Text = c;
        if (Repeater1.Items.Count <= 0)
        {
            Label1.Text = "没有找到此类商品";
        }
        but_1.Click += but_1_Click;
        but_x.Click += but_x_Click;
        but_s.Click += but_s_Click;
        but_0.Click += but_0_Click;
        DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;
        Button1.Click += Button1_Click;    //各种事件
    }

    void Button1_Click(object sender, EventArgs e)
    {
        int a = new taobaodata().selectcount(count());
        List<taobao> tlist = tiaojian();
        Repeater1.DataSource = tlist;
        Repeater1.DataBind();
        tiaoshu = a;
        yeshu_2.Text = suan().ToString();
        //yeshu_1.Text = "1";

        but();
    }

    void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        yeshu_1.Text = DropDownList1.SelectedValue;
        if (yeshu_1.Text != "请选择")
        {
            int a = new taobaodata().selectcount(count());
            List<taobao> tlist = tiaojian();
            Repeater1.DataSource = tlist;
            Repeater1.DataBind();
            tiaoshu = a;
            yeshu_2.Text = suan().ToString();
        }
        but();
    }

    void but_0_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(yeshu_1.Text) < Convert.ToInt32(yeshu_2.Text))
        {
            yeshu_1.Text = yeshu_2.Text;

            if (yeshu_1.Text != "请选择")
            {
                int a = new taobaodata().selectcount(count());
                List<taobao> tlist = tiaojian();
                Repeater1.DataSource = tlist;
                Repeater1.DataBind();
                tiaoshu = a;
                yeshu_2.Text = suan().ToString();
            }
        }
        but();
    }

    void but_s_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(yeshu_1.Text) > 1)
        {
            int a = Convert.ToInt32(yeshu_1.Text) - 1;
            yeshu_1.Text = a.ToString();

            if (yeshu_1.Text != "请选择")
            {
                int aa = new taobaodata().selectcount(count());
                List<taobao> tlist = tiaojian();
                Repeater1.DataSource = tlist;
                Repeater1.DataBind();
                tiaoshu = aa;
                yeshu_2.Text = suan().ToString();
            }
        }
        but();
    }
    void but_x_Click(object sender, EventArgs e)
    {
        tiaojian();
        if (Convert.ToInt32(yeshu_1.Text) < Convert.ToInt32(yeshu_2.Text))
        {
            int a = 1 + Convert.ToInt32(yeshu_1.Text);
            yeshu_1.Text = a.ToString();

            if (yeshu_1.Text != "请选择")
            {
                int aa = new taobaodata().selectcount(count());
                List<taobao> tlist = tiaojian();
                Repeater1.DataSource = tlist;
                Repeater1.DataBind();
                tiaoshu = aa;
                yeshu_2.Text = suan().ToString();
            }
            but();
        }

    }

    void but_1_Click(object sender, EventArgs e)
    {

        yeshu_1.Text = "1";

        if (yeshu_1.Text != "请选择")
        {
            int aa = new taobaodata().selectcount(count());
            List<taobao> tlist = tiaojian();
            Repeater1.DataSource = tlist;
            Repeater1.DataBind();
            tiaoshu = aa;
            yeshu_2.Text = suan().ToString();
        }

        but();
    }
    //计算一共有多少页
    public int suan()
    {
        int a = 0;
        yecount = Convert.ToInt32(Math.Ceiling(tiaoshu / Convert.ToDecimal(yehascount)));
        a = yecount;
        return a;
    }

    public void but()
    {
        if (Convert.ToInt32(yeshu_1.Text) <= 1)
        {
            but_1.Visible = false;
            but_s.Visible = false;
            but_x.Visible = true;
            but_0.Visible = true;

        }
        else if (Convert.ToInt32(yeshu_1.Text) >= Convert.ToInt32(yeshu_2.Text))
        {
            but_x.Visible = false;
            but_0.Visible = false;
            but_1.Visible = true;
            but_s.Visible = true;
        }

        if (Convert.ToInt32(yeshu_1.Text) > 1 && Convert.ToInt32(yeshu_1.Text) < Convert.ToInt32(yeshu_2.Text))
        {
            but_1.Visible = true;
            but_s.Visible = true;
            but_x.Visible = true;
            but_0.Visible = true;
        }
    }


    public List<taobao> tiaojian()
    {
        List<taobao> tlist = new List<taobao>();
        string dd = "";
        string c = "";  //拼接数据库查询字符串;

        bool ok = false;
        if (TextBox1.Text.Trim().Length > 0)
        {
            if (!ok)
            {
                c += "and ids = " + TextBox1.Text.Trim() + " ";
                dd += " where ids = " + TextBox1.Text.Trim() + " ";
                ok = true;
            }
            else
            {
                c += "and ids = " + TextBox1.Text.Trim() + " ";
                dd += "and ids = " + TextBox1.Text.Trim() + " ";
            }

        }
        if (TextBox2.Text.Trim().Length > 0)
        {
            if (ok)
            {
                c += "and name like ‘%" + TextBox2.Text.Trim() + "%‘ ";
                dd += "and name like ‘%" + TextBox2.Text.Trim() + "%‘";
            }
            else
            {
                c += "and name like ‘%" + TextBox2.Text.Trim() + "%‘  ";
                dd += "where name like ‘%" + TextBox2.Text.Trim() + "%‘ ";
                ok = true;
            }
        }

        if (TextBox3.Text.Trim().Length > 0)
        {
            if (ok)
            {
                c += "and lprice " + DropDownList2.SelectedValue + " " + TextBox3.Text + "";
                dd += "and lprice " + DropDownList2.SelectedValue + " " + TextBox3.Text + "";
            }
            else
            {
                c += "and lprice " + DropDownList2.SelectedValue + " " + TextBox3.Text + " ";
                dd += "where lprice " + DropDownList2.SelectedValue + " " + TextBox3.Text + " ";
                ok = true;
            }
        }

        string asfasdf = "select top " + yehascount + " * from taobao where ids not in(select top " + yehascount * (Convert.ToInt32(yeshu_1.Text) - 1) + " ids from taobao " + dd + ") " + c + " ";

        tlist = new taobaodata().selectall(asfasdf);
        return tlist;

    }


    public string count()
    {
        string dd = "select Count(*)from taobao ";

        bool ok = false;
        if (TextBox1.Text.Trim().Length > 0)
        {
            if (!ok)
            {
                dd += " where ids = " + TextBox1.Text.Trim() + " ";
                ok = true;
            }
            else
            {
                dd += "and ids = " + TextBox1.Text.Trim() + " ";
            }

        }
        if (TextBox2.Text.Trim().Length > 0)
        {
            if (ok)
            {
                dd += "and name like ‘%" + TextBox2.Text.Trim() + "%‘";
            }
            else
            {
                dd += "where name like ‘%" + TextBox2.Text.Trim() + "%‘ ";
                ok = true;
            }
        }

        if (TextBox3.Text.Trim().Length > 0)
        {
            if (ok)
            {
                dd += "and lprice " + DropDownList2.SelectedValue + " " + TextBox3.Text + "";
            }
            else
            {
                dd += "where lprice " + DropDownList2.SelectedValue + " " + TextBox3.Text + " ";
                ok = true;
            }
        }


        return dd;

    }
}

 

分页功能 与 分类查询功能合并