简体   繁体   中英

Custom paging with gridview and linqdatasource

I have a problem which in theory should be very simple but i spent 3 hours now not being able to figure it out. I have a gridview and lingdatasource and i try to implement custom paging. No matter what i do i seem to be stuck with a pagesize of 25! Code below Thanks!

aspx page

<asp:GridView ID="gvCustomerList" SkinID="StandardGridView" AutoGenerateColumns="false" DataSourceID="LqCustomerSource"
         AllowPaging="false" AllowSorting="false" PageSize="50" runat="server" OnSorting="gvCustomerList_OnSorting"
        OnRowCommand="gvSupplierCustomerList_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="Customer Id" HeaderStyle-HorizontalAlign="Left" SortExpression="CustomerId">
                <ItemTemplate>
                    <asp:Label ID="lblCustomerId" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="First Name" HeaderStyle-HorizontalAlign="Left" SortExpression="FirstName">
                <ItemTemplate>
                    <asp:Label ID="lblFirstName" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).FirstName %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Last Name" HeaderStyle-HorizontalAlign="Left" SortExpression="LastName">
                <ItemTemplate>
                    <asp:Label ID="lblLastName" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).LastName %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Details" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:HyperLink ID="lnkDetails" Text="Details" NavigateUrl='<%# "~/BackOffice/Customers/CustomerDetails.aspx?CustomerId=" + ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="Ask" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:HyperLink ID="lnkAsk" Text="Ask" NavigateUrl='<%# "~/BackOffice/Customers/Ask.aspx?CustomerId=" + ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>'  runat="server" />
                </ItemTemplate>
            </asp:TemplateField> 

        </Columns>
    </asp:GridView>
</div>
<asp:LinqDataSource ID="LqCustomerSource" AutoPage="false" runat="server" 
OnSelecting="LqCustomerSource_Selecting" AutoSort="False">
</asp:LinqDataSource>

code behind

protected void LqCustomerSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    try
    {
        CustomerDataManager manager = new CustomerDataManager();
        string searchName = "";// txtCustomerName.Text.Trim();
        int count = 0;

        List<Customer> customerList = manager.GetSelectedCustomers(gvCustomerList.PageIndex * 50, 50, searchName, SortExpression, SortDirectionForCustomers, out count);
        //List<CustomerInsuranceInfo> infoList = manager.GetCustomerInsuranceDataFromList(customerList);
        lblRows.Text = count.ToString();

        e.Arguments.StartRowIndex = 0;
        e.Arguments.MaximumRows = 50;
        e.Arguments.TotalRowCount = count;

        if (customerList == null)
            e.Cancel = true;
        else
            e.Result = customerList;
    }
    catch (Exception ex)
    {
        ApplicationLogBO.Log(ex);
       // Alert(WebsiteUtil.StandardErrorMessage);
        e.Cancel = true;
    }

}

Ok, it looks like you need to set it in the code behind in LqCustomerSource_Selecting

gvCustomerList.PageSize = 50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM