gridview html asp.net中的分页

时间:2016-05-18 02:28:24

标签: c# html asp.net gridview pagination

我希望在gridview中有一个分页功能。我设置了“allow pagination = true”并从aspsnippets.com复制了一些代码。但不知何故,我的代码无效。这是我的html gridview:

顺便说一句,我的gridview是ins.content。 (不确定它是否与它有关,为什么它不起作用)

<asp:GridView ID="gvCustomerOrders" runat="server" Width="940px"  HorizontalAlign="Center"
            AutoGenerateColumns="false"   AllowPaging="True" CssClass="table table-hover table-striped" OnPageIndexChanging="OnPaging" 
            onselectedindexchanged="gvCustomerOrders_SelectedIndexChanged">

    <Columns>
        <asp:BoundField DataField ="Id" HeaderText ="Id" ItemStyle-Width="100" />
        <asp:BoundField DataField ="CustomerName" HeaderText ="Name" />    
        <asp:BoundField DataField ="CustomerPhoneNo" HeaderText ="PhoneNo" />
        <asp:BoundField DataField ="TotalProducts" HeaderText ="Products" />
        <asp:BoundField DataField ="TotalPrice" HeaderText ="Price" />


        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ='<%# Eval("Id", "~/Admin/OrderDetails.aspx?Id={0}") %>'
                                    Text="View Details" Target="_blank" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>

这是背后的代码:

private void GetOrderList()
{
    ShoppingCart k = new ShoppingCart()
    {
        Flag = 0
    };
    DataTable dt = k.GetOrderList();

    gvCustomerOrders.DataSource = dt;
    gvCustomerOrders.DataBind();
    gvCustomerOrders.HeaderRow.TableSection = TableRowSection.TableHeader;
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    gvCustomerOrders.PageIndex = e.NewPageIndex;
    this.GetOrderList();
}

编辑:

我让这个程序奏效了。但不幸的是我必须删除这些代码行:

if (Session["IslandGasAdminST"] != null)
        {

            if (!IsPostBack)
            {
                Label1.Text = "- Supply & Transport Manager";
                GetOrderList();
            }

            else
            {
                Response.Write("<script>alert('Supply & Transport Manager credentials needed'); window.location.href='LogIn.aspx';</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('Supply & Transport Manager credentials needed'); window.location.href='LogIn.aspx';</script>");
        }

它应该是这样的:

protected void Page_Load(object sender, EventArgs e)
    {

            if (!IsPostBack)
            {
                Label1.Text = "- Supply & Transport Manager";
                GetOrderList();
            }


    }

关于如何实现会话部分的任何技巧?对于特定的登录只能是这个页面吗?

2 个答案:

答案 0 :(得分:1)

也许您想检查在分页时是否有任何客户端异常被抛出。使用您自己的示例,我能够无问题地来回导航页面。

我在我的示例中唯一的区别是 - 设置一些测试数据并设置PageSize = 1

<asp:GridView ID="gvCustomerOrders" runat="server" HorizontalAlign="Center"
                AutoGenerateColumns="false" AllowPaging="True" CssClass="table table-hover table-striped" OnPageIndexChanging="OnPaging"
                OnSelectedIndexChanged="gvCustomerOrders_SelectedIndexChanged" PageSize="1">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" />
                    <asp:BoundField DataField="CustomerName" HeaderText="Name" />
                    <asp:BoundField DataField="CustomerPhoneNo" HeaderText="PhoneNo" />
                    <asp:BoundField DataField="TotalProducts" HeaderText="Products" />
                    <asp:BoundField DataField="TotalPrice" HeaderText="Price" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Id", "~/Admin/OrderDetails.aspx?Id={0}") %>'
                                Text="View Details" Target="_blank" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

代码背后:

    private void GetOrderList()
    {
        DataTable dt = GetOrderListData();
        gvCustomerOrders.DataSource = dt;
        gvCustomerOrders.DataBind();
        gvCustomerOrders.HeaderRow.TableSection = TableRowSection.TableHeader;
    }

    private DataTable GetOrderListData()
    {
        var dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("CustomerName");
        dt.Columns.Add("CustomerPhoneNo");
        dt.Columns.Add("TotalProducts", typeof(int));
        dt.Columns.Add("TotalPrice", typeof(decimal));
        dt.Rows.Add(1, "TEST1", "123123", 10, 100M);
        dt.Rows.Add(2, "TEST2", "123123", 20, 200M);
        dt.Rows.Add(3, "TEST3", "123123", 30, 300M);
        dt.Rows.Add(4, "TEST4", "123123", 40, 400M);
        return dt;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetOrderList();
        }
    }

答案 1 :(得分:0)

想通了。最终代码是:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["IslandGasAdminST"] != null)
        {

            if (!IsPostBack)
            {
                Label1.Text = "- Supply & Transport Manager";
                GetOrderList();
            }

            //else
            //{
            //    Response.Write("<script>alert('Supply & Transport Manager credentials needed'); window.location.href='LogIn.aspx';</script>");
            //}
        }
        else
        {
            Response.Write("<script>alert('Supply & Transport Manager credentials needed'); window.location.href='LogIn.aspx';</script>");
        }
    }