加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

ASP.Net2.0 GridView 多列排序,显示排序图标,分页

发布时间:2020-03-20 23:19:58 所属栏目:Asp教程 来源:互联网
导读:ASP.Net2.0 GridView 多列排序,显示排序图标,分页

<script runat="server">
    void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        CustomersGridView.PageIndex = pageList.SelectedIndex;
    }
    void CustomersGridView_DataBound(Object sender, EventArgs e)
    {
        GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
        LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
        LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
        LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
        LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
        if (CustomersGridView.PageIndex == 0)
        {
            linkBtnFirst.Enabled = false;
            linkBtnPrev.Enabled = false;
        }
        else if (CustomersGridView.PageIndex == CustomersGridView.PageCount-1)
        {
            linkBtnLast.Enabled  = false;
            linkBtnNext.Enabled = false;
        }
        else if (CustomersGridView.PageCount<=0)
        {
            linkBtnFirst.Enabled = false;
            linkBtnPrev.Enabled = false;
            linkBtnNext.Enabled = false;
            linkBtnLast.Enabled = false;
        }
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
        if (pageList != null)
        {
            for (int i = 0; i < CustomersGridView.PageCount; i++)
            {
                int pageNumber = i + 1;
                ListItem item = new ListItem(pageNumber.ToString() + "http://www.jb51.net/" + CustomersGridView.PageCount.ToString(), pageNumber.ToString());
                if (i == CustomersGridView.PageIndex)
                {
                    item.Selected = true;
                }
                pageList.Items.Add(item);
            }
        }
        if (pageLabel != null)
        {
            int currentPage = CustomersGridView.PageIndex + 1;
            pageLabel.Text = "当前页: " + currentPage.ToString() +
              " / " + CustomersGridView.PageCount.ToString();
        }
    }
</script>
<html>
<body>
    <form runat="server">
        <h3>
            GridView PagerTemplate Example</h3>
        <asp:WebGridView DataSourceID="CustomersSqlDataSource" AutoGenerateColumns="true"
            AllowPaging="true" OnDataBound="CustomersGridView_DataBound" SortAscImageUrl="~imagesarrow-up.gif" SortDescImageUrl="~imagesarrow-down.gif" runat="server" AllowSorting="True">
            <PagerStyle ForeColor="Blue" BackColor="LightBlue" />
            <PagerTemplate>
                <table>
                    <tr>
                        <td>
                            <asp:Label ForeColor="Blue" Text="页码:" runat="server" />
                            <asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
                                runat="server" />
                            <asp:LinkButton CommandName="Page" CommandArgument="First" runat="server">首页</asp:LinkButton>
                            <asp:LinkButton CommandName="Page" CommandArgument="Prev" runat="server">上一页</asp:LinkButton>
                            <asp:LinkButton CommandName="Page" CommandArgument="Next" runat="server">下一页</asp:LinkButton>
                            <asp:LinkButton CommandName="Page" CommandArgument="Last" runat="server">末页</asp:LinkButton>
                        </td>
                        <td>
                            <asp:Label ForeColor="Blue" runat="server" />
                        </td>
                    </tr>
                </table>
            </PagerTemplate>
       </asp:WebGridView>
        <asp:SqlDataSource SelectCommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
            ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server">
        </asp:SqlDataSource>
    </form>
</body>
</html>

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读