1. 程式人生 > >分頁功能 與 分類查詢功能合並

分頁功能 與 分類查詢功能合並

nat hidden 後臺 item ble fas turn items where

功能的合並 首要的就是要找出兩個功能的共同點。這裏主要是用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=false;" MaxLength="
11"></asp:TextBox> 名稱:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 價格:<asp:DropDownList ID="DropDownList2" runat="server"> <asp:ListItem Text="大於" Value=">"></asp:ListItem> <asp:ListItem Text="
小於" Value="<"></asp:ListItem> <asp:ListItem Text="大於等於" Value=">="></asp:ListItem> <asp:ListItem Text="小於等於" Value="<="></asp:ListItem> <asp:ListItem Text="等於" Value="="></asp:ListItem> </asp:DropDownList>&nbsp;<asp:TextBox ID="
TextBox3" runat="server" onkeypress="if(event.keyCode<48||event.keyCode>57)event.returnValue=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=""></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;

    }
}

分頁功能 與 分類查詢功能合並