繁体   English   中英

使用LinqDataSource和Formview进行自定义分页

[英]Custom Paging With LinqDataSource & Formview

我正在尝试使用LinqDataSource的Selecting事件为FormView过滤数据,但是我似乎无法正确地将其分页。 我只能设法在FormView中显示一条记录,但分页控件却没有显示。 我在LinqDataSource Selecting事件中具有以下代码:

e.Arguments.StartRowIndex = 0;
e.Arguments.MaximumRows = 1;

var result = db.Personnels.AsQueryable();

if (!String.IsNullOrEmpty(txtFirstName.Text))
{
    result = result.Where(r => r.First_Name.Contains(txtFirstName.Text));
}

if (!String.IsNullOrEmpty(txtLastName.Text))
{
    result = result.Where(r => r.Last_Name.Contains(txtLastName.Text));
}

e.Arguments.TotalRowCount = result.Count();

e.Result = result.Skip(fvMain.PageIndex).Take(1);

如上所述,此代码有效,但是仅显示一条记录,并且分页控件未显示在FormView上。 我也尝试使用以下方法修改e.Result ,但是我得到的Object reference not set to an instance of an object. 例外:

e.Result = result;

使用LinqDataSource的selecting事件分页FormView的正确方法是什么?


编辑1

根据要求,这是FormView和LinqDataSource的标记:

<asp:FormView ID="fvMain" runat="server" CssClass="full" 
    DataKeyNames="Worker_ID" DataSourceID="ldsMain" DefaultMode="Edit" 
    AllowPaging="True" onitemupdating="fvMain_ItemUpdating">
    <EditItemTemplate>
        <table class="pad5 full">
            <tr>
                <td class="field-name" style="width: 100px">Worker ID:</td>
                <td style="width: 80px"><asp:TextBox ID="txtWorkerID" runat="server" Text='<%#Eval("Worker_ID") %>' ReadOnly="true" style="width: 75px" /></td>
                <td class="right"><input type="button" value="Injuries/LTA/WCB for this Person" onclick="openModalColorbox('Injuries.aspx?id='+$('#plcMain_fvMain_txtWorkerID').val(), 'Injuries')" /></td>
            </tr>
        </table>
        <table class="pad5 full">
            <tr>
                <td class="field-name">Type Of Person:</td>
                <td colspan="3">
                    <cc1:DataBindDropDownList ID="cboTypeOfPerson" runat="server" 
                        AppendDataBoundItems="True" DataSourceID="ldsPersonTypes" 
                        DataTextField="Type_of_Person" DataValueField="Type_of_Person" 
                        SelectedValue='<%#Bind("Type_Of_Person") %>'>
                        <asp:ListItem Text="" Value="" />
                    </cc1:DataBindDropDownList>
                    <asp:LinqDataSource ID="ldsPersonTypes" runat="server" 
                        ContextTypeName="PRIDE.PRIDEDataContext" EntityTypeName="" 
                        OrderBy="Type_of_Person" TableName="Personnel_Types">
                    </asp:LinqDataSource>
                </td>
            </tr>
            <tr>
                <td class="field-name">Employee Number:</td>
                <td><asp:TextBox ID="txtEmployeeNumber" runat="server" Text='<%#Bind("Employee_Number") %>' /></td>
                <td class="field-name">SIN:</td>
                <td><asp:TextBox ID="txtSIN" runat="server" Text='<%#Bind("SIN") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Last Name:</td>
                <td><asp:TextBox ID="txtLastName" runat="server" Text='<%#Bind("Last_Name") %>' /></td>
                <td class="field-name">Previous Last Name:</td>
                <td><asp:TextBox ID="txtPreviousLastName" runat="server" Text='<%#Bind("Previous_Last_Name") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">First Name:</td>
                <td><asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("First_Name") %>' /></td>
                <td class="field-name">Marital Status:</td>
                <td>
                    <cc1:DataBindDropDownList ID="DropDownList1" runat="server" 
                        AppendDataBoundItems="True" SelectedValue='<%# Bind("Marital_Status") %>' 
                        DataSourceID="ldsMaritalStatuses" DataTextField="Marital_Status" 
                        DataValueField="Marital_Status">
                        <asp:ListItem Text="" Value="" />
                    </cc1:DataBindDropDownList>
                    <asp:LinqDataSource ID="ldsMaritalStatuses" runat="server" 
                        ContextTypeName="PRIDE.PRIDEDataContext" EntityTypeName="" 
                        OrderBy="Marital_Status" TableName="List____Employee__Marital_Status">
                    </asp:LinqDataSource>
                </td>
            </tr>
            <tr>
                <td class="field-name">Division:</td>
                <td>
                    <cc1:DataBindDropDownList ID="cboDivision" runat="server" 
                        AppendDataBoundItems="True" SelectedValue='<%# Bind("Division") %>' 
                        DataSourceID="ldsDivisions" DataTextField="Division" 
                        DataValueField="Division">
                        <asp:ListItem Text="" Value="" />
                    </cc1:DataBindDropDownList>
                    <asp:LinqDataSource ID="ldsDivisions" runat="server" 
                        ContextTypeName="PRIDE.PRIDEDataContext" EntityTypeName="" GroupBy="Division" 
                        OrderBy="Division" Select="new (key as Division, it as Areas)" 
                        TableName="Areas">
                    </asp:LinqDataSource>
                </td>
                <td class="field-name">DOB:</td>
                <td>
                    <asp:TextBox ID="txtDOB" runat="server" Text='<%#Bind("DOB", "{0:dd MMM yyyy}") %>' />
                    <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDOB" Format="dd MMM yyyy" />
                </td>
            </tr>
            <tr>
                <td class="field-name">Department:</td>
                <td>
                    <cc1:DataBindDropDownList ID="cboDepartment" runat="server" 
                        AppendDataBoundItems="True" SelectedValue='<%# Bind("Department") %>' 
                        DataSourceID="ldsDepartments" DataTextField="Department" 
                        DataValueField="Department">
                        <asp:ListItem Text="" Value="" />
                    </cc1:DataBindDropDownList>
                    <asp:LinqDataSource ID="ldsDepartments" runat="server" 
                        ContextTypeName="PRIDE.PRIDEDataContext" EntityTypeName="" GroupBy="Department" 
                        OrderBy="Department" Select="new (key as Department, it as Areas)" 
                        TableName="Areas">
                    </asp:LinqDataSource>
                </td>
                <td class="field-name">Terminated:</td>
                <td>
                    <cc1:DataBindDropDownList ID="cboTerminated" runat="server" 
                        SelectedValue='<%# Bind("Terminated") %>'>
                        <asp:ListItem Text="" Value="" />
                        <asp:ListItem Text="Yes" Value="Yes" />
                        <asp:ListItem Text="No" Value="No" />
                    </cc1:DataBindDropDownList>
                </td>
            </tr>
            <tr>
                <td class="field-name">Occupation:</td>
                <td>
                    <cc1:DataBindDropDownList ID="cboOccupation" runat="server" 
                        SelectedValue='<%# Bind("Occupation") %>' AppendDataBoundItems="True" 
                        DataSourceID="ldsOccupations" DataTextField="Occupation" 
                        DataValueField="Occupation">
                        <asp:ListItem Text="" Value="" />
                    </cc1:DataBindDropDownList>

                    <asp:LinqDataSource ID="ldsOccupations" runat="server" 
                        ContextTypeName="PRIDE.PRIDEDataContext" EntityTypeName="" OrderBy="Occupation" 
                        TableName="List____Employee__Occupations">
                    </asp:LinqDataSource>
                </td>
                <td class="field-name">Team:</td>
                <td>
                    <cc1:DataBindDropDownList ID="cboTeam" runat="server" 
                        SelectedValue='<%# Bind("Shift") %>' AppendDataBoundItems="True" 
                        DataSourceID="ldsShifts" DataTextField="Shift" 
                        DataValueField="Shift">
                        <asp:ListItem Text="" Value="" />
                    </cc1:DataBindDropDownList>

                    <asp:LinqDataSource ID="ldsShifts" runat="server" 
                        ContextTypeName="PRIDE.PRIDEDataContext" EntityTypeName="" OrderBy="Shift" 
                        TableName="List____Employee__Shifts">
                    </asp:LinqDataSource>
                </td>
            </tr>
            <tr>
                <td class="field-name">Lock Number:</td>
                <td><asp:TextBox ID="txtLockNumber" runat="server" Text='<%#Bind("Lock_Number") %>' /></td>
                <td class="field-name">Address:</td>
                <td><asp:TextBox ID="txtAddress" runat="server" Text='<%#Bind("Address") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">City:</td>
                <td><asp:TextBox ID="txtCity" runat="server" Text='<%#Bind("City") %>' /></td>
                <td class="field-name">Company:</td>
                <td><asp:TextBox ID="txtCompany" runat="server" Text='<%#Bind("Company") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Province:</td>
                <td><asp:TextBox ID="txtProvince" runat="server" Text='<%#Bind("Province") %>' /></td>
                <td class="field-name">Company Contact:</td>
                <td><asp:TextBox ID="txtCompanyContact" runat="server" Text='<%#Bind("Company_Contact") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Postal:</td>
                <td><asp:TextBox ID="txtPostal" runat="server" Text='<%#Bind("Postal") %>' /></td>
                <td class="field-name">Phone:</td>
                <td><asp:TextBox ID="txtPhone" runat="server" Text='<%#Bind("Phone") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">HCN:</td>
                <td><asp:TextBox ID="txtHCN" runat="server" Text='<%#Bind("HCN") %>' /></td>
                <td class="field-name">HCN Province:</td>
                <td><asp:TextBox ID="txtHCNProvince" runat="server" Text='<%#Bind("HCN_Province") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Comments:</td>
                <td colspan="3">
                    <asp:TextBox ID="txtComments" runat="server" Text='<%#Bind("Comments") %>' TextMode="MultiLine" Rows="3" />
                </td>
            </tr>
        </table>

        <h2>H.R.I.S.</h2>
        <table class="pad5 full">
            <tr>
                <td class="field-name">NOK Name:</td>
                <td><asp:TextBox ID="txtNOKName" runat="server" Text='<%#Bind("NOK_Name") %>' /></td>
                <td class="field-name">NOK Relation:</td>
                <td>
                    <cc1:DataBindDropDownList ID="cboNOKRelation" runat="server" 
                        SelectedValue='<%# Bind("NOK_Relation") %>' AppendDataBoundItems="True" 
                        DataSourceID="ldsNOKRelations" DataTextField="Relationship" 
                        DataValueField="Relationship">
                        <asp:ListItem Text="" Value="" />
                    </cc1:DataBindDropDownList>

                    <asp:LinqDataSource ID="ldsNOKRelations" runat="server" 
                        ContextTypeName="PRIDE.PRIDEDataContext" EntityTypeName="" 
                        OrderBy="Relationship" TableName="List____Employee__Relations">
                    </asp:LinqDataSource>
                </td>
            </tr>
            <tr>
                <td class="field-name">NOK Address:</td>
                <td><asp:TextBox ID="txtNOKAddress" runat="server" Text='<%#Bind("NOK_Address") %>' /></td>
                <td class="field-name">NOK City:</td>
                <td><asp:TextBox ID="txtNOKCity" runat="server" Text='<%#Bind("NOK_City") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">NOK Province:</td>
                <td><asp:TextBox ID="txtNOKProvince" runat="server" Text='<%#Bind("NOK_Province") %>' /></td>
                <td class="field-name">NOK Postal:</td>
                <td><asp:TextBox ID="txtNOKPostal" runat="server" Text='<%#Bind("NOK_Postal") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">NOK Phone:</td>
                <td><asp:TextBox ID="txtNOKPhone" runat="server" Text='<%#Bind("NOK_Phone") %>' /></td>
            </tr>
        </table>

        <div class="center">
            <asp:Button ID="btnSave" runat="server" Text="Save Changes" 
                onclick="btnSave_Click" />
        </div>
    </EditItemTemplate>
    <PagerSettings Mode="NextPreviousFirstLast" 
                FirstPageText="&amp;lt;&amp;lt; First" LastPageText="Last &amp;gt;&amp;gt;" 
                NextPageText="Next &amp;gt;" PreviousPageText="&amp;lt; Previous" 
                Position="TopAndBottom" />
            <PagerStyle CssClass="pager" />
</asp:FormView>
<asp:LinqDataSource ID="ldsMain" runat="server" 
    ContextTypeName="PRIDE.PRIDEDataContext" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" EntityTypeName="" 
    TableName="Personnels" onselecting="ldsMain_Selecting">
    <UpdateParameters>
        <asp:Parameter ConvertEmptyStringToNull="true" Name="Employee_Number" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter ConvertEmptyStringToNull="true" Name="Employee_Number" />
    </InsertParameters>
</asp:LinqDataSource>

MSDN中,我们可以看到LinqDataSourceAutoPage属性默认设置为true 从文档:

当AutoPage属性设置为true时,LinqDataSource控件仅检索数据绑定控件中一页的足够记录。 它使用Skip<TSource>Take<TSource>方法来检索当前页面的记录。

所有这些的结果是,您需要更改一些内容才能使分页工作。

  1. 当前,您正在设置e.Arguments.StartRowIndex = 0; 您不需要每次都这样做,因为它会干扰分页。
  2. e.Result = result.Skip(fvMain.PageIndex).Take(1); 也没有必要。 只是e.Result = result; 应该没事。

我认为这两件事的结合导致了您的问题。 但是,我们现在需要处理其中一个过滤器启动的情况。

第一个问题是我们需要将以前应用的过滤器保留在ViewState中,否则我们将无法分辨何时应用了过滤器以及何时使用了现有过滤器。

启发于将过滤器应用于连接到GridView的linqdatasource之后,单击“编辑”使我回到旧数据 ,在页面中添加一个属性(您的姓氏将需要另一个):

public string FirstNameFilter 
{
    get
    {
        return (string)this.ViewState["FirstNameFilter"] ?? string.Empty;
    }

    set
    {
        this.ViewState["FirstNameFilter"] = value;
    }
}

然后在您的Selecting事件处理程序中,运行以下代码:

var result = db.Personnels.AsQueryable();

if (!string.IsNullOrEmpty(txtFirstName.Text))
{
    if (this.FirstNameFilter != txtFirstName.Text)
    {
        this.FirstNameFilter = txtFirstName.Text;
        e.Arguments.StartRowIndex = 0;
    }

    if (!string.IsNullOrEmpty(this.FirstNameFilter))
    {
        result = result.Where(r => r.First_Name.Contains(txtFirstName.Text));
    }
}

if (!string.IsNullOrEmpty(txtLastName.Text))
{
    if (this.LastNameFilter != txtLastName.Text)
    {
        this.LastNameFilter = txtLastName.Text;
        e.Arguments.StartRowIndex = 0;
    }

    if (!string.IsNullOrEmpty(this.LastNameFilter))
    {
        result = result.Where(r => r.First_Name.Contains(txtLastName.Text));
    }
}

e.Arguments.TotalRowCount = result.Count();

e.Result = result;

我不确定为什么,但是我的问题通过将读取e.Result = result...的行更改为e.Result = result... e.Result = result.Skip(0)得以e.Result = result.Skip(0) 完成后,分页可以完美地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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