ListView排序(code behind)

摘要:ListView排序(code behind)

.ASPX 內容


 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:ListView ID="ListView1" runat="server" 
            OnPagePropertiesChanged="ListView1_PagePropertiesChanged" 
            onsorting="ListView1_Sorting">
            <LayoutTemplate>
                <table border="1">
                    <!--欄位名稱-->
                    <thead>
                        <tr>
                            <th>
                                <asp:LinkButton Text="主機名稱" runat="server" CommandName="sort" CommandArgument="HOST_NAME" />
                            </th>
                            <th>
                                <asp:LinkButton Text="主機識別碼" runat="server" CommandName="sort" CommandArgument="HOSTID" />
                            </th>
                            <th>
                                <asp:LinkButton Text="主機狀態" runat="server" CommandName="sort" CommandArgument="STATUS"/>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <%--<asp:PlaceHolder runat="server" ID="itemPlaceholder" />--%>
                        <tr runat="server" id="itemPlaceholder" />
                    </tbody>
                    <!--分頁-->
                    <tr runat="server">
                        <td runat="server">
                            <asp:DataPager ID="_moviesGridDataPager" runat="server" PageSize="2">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowLastPageButton="false"
                                        ShowNextPageButton="false" ShowPreviousPageButton="false" />
                                    <asp:NumericPagerField />
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="true" ShowNextPageButton="false"
                                        ShowPreviousPageButton="false" />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <%# Eval("HOST_NAME") %>
                    </td>
                    <td>
                        <%# Eval("HOSTID") %>
                    </td>
                    <td>
                        <%# Eval("STATUS") %>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>

 

.cs 後端內容


public partial class ListViewFrom : System.Web.UI.Page
    {
        DBHelper db = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBHelper();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //將排序的ViewState設定為null,新的查詢才會為初始的排序
            ViewState["SortExpression"] = null;
            this.Bind(ViewState["SortExpression"]);
        }

        private void Bind(object oSortExpression)
        {
            //當查詢語法的TextBox為空時return
            if (this.TextBox1.Text.Trim().Equals("")) return;
            string sSQL = this.TextBox1.Text.Trim();
            if (oSortExpression != null) //當傳入的排序參數不為null將串成SQL語法
                sSQL = this.TextBox1.Text.Trim() + oSortExpression.ToString();

            //進行查詢
            DataSet ds = db.QueryData(sSQL);
            //設定ListView的資料來源
            this.ListView1.DataSource = ds;
            //進行資料Bind動作
            this.ListView1.DataBind();
        }

        protected void ListView1_PagePropertiesChanged(object sender, EventArgs e)
        {
            //取得ListView的分頁元件
            DataPager dp = this.ListView1.FindControl("_moviesGridDataPager") as DataPager;
            //取得每頁所要顯示的筆數
            int pagesize = dp.PageSize;
            //取得所要瀏覽的分頁號碼
            int startRowIndex = dp.StartRowIndex;
            //取得資料筆數
            int totalRowCount = dp.TotalRowCount;
            //設定分頁元件所要顯示的分頁號和每頁所顯示的筆數
            dp.SetPageProperties(startRowIndex, pagesize, true);
            //資料設定
            this.Bind(ViewState["SortExpression"]);
        }

        protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
        {
            string sSortDirection = "ASC";
            //當ViewState的Key為SortExpression非null時
            if (ViewState["SortExpression"] != null)
            {
                //判斷SortExpression的Value字串是否包含ASC
                if (ViewState["SortExpression"].ToString().Contains(sSortDirection))
                {
                    //代表該欄位要設為DESC
                    sSortDirection = " DESC ";
                }
            }
            else
            {
                sSortDirection = " DESC ";
            }
            //SortExpression的Value排序 e.SortExpression為要排序的欄位,sSortDirection為本次的排序方式
            ViewState["SortExpression"] = " ORDER BY " + e.SortExpression + " " + sSortDirection;
            this.Bind(ViewState["SortExpression"]);
        }
    }