2013-02-13 32 views
0

我想使用LinqDataSource的Selecting事件來過濾FormView中的數據,但是我似乎無法將其正確地分頁。我可以設法在FormView中只顯示一條記錄,但分頁控件不顯示。我在使用LinqDataSource Selecting事件下面的代碼:使用LinqDataSource和Formview自定義分頁

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的選擇事件一個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> 
+0

當您運行該代碼時,e.Arguments.TotalRowCount的值是多少? – 2013-02-20 05:18:03

+0

@nick_w如果我將文本框留空('txtFirstName'和'txtLastName'),'TotalRowCount'爲3,376 – Hoff 2013-02-20 14:01:43

+0

乾杯。你還可以加入你的標記嗎? – 2013-02-20 19:47:09

回答

0

我不知道爲什麼,但我的問題是通過將e.Result = result...更改爲e.Result = result.Skip(0)來修復的。這樣做後,分頁完美。

0

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中,否則我們將無法確定篩選器何時剛剛被應用,當它是一個現有的。

通過After applying a filter to linqdatasource connected to GridView ,clicking edit bring me back to the old data的啓發,一個屬性添加到您的網頁(您需要一個又一個的姓氏):

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; 
+0

感謝您的回答!不幸的是,我仍然得到'對象引用未設置爲對象的一個​​實例.'與上述代碼的異常。看起來,即使當我改變'e.Result = result.Take(10)'它仍然有效時,當我傳遞整個'result'對象時它就不喜歡。這使我相信,也許我在我的FormView模板中有一個綁定的問題?我無法發佈所有代碼,因爲它像800行長,但無論如何,它看起來像我有一個比我想象的不同的問題。我會獎勵你的時間和精力,如果你有任何其他想法,請讓我知道! – Hoff 2013-02-21 15:03:14

+0

呃,我剛剛意識到我昨天粘貼了錯誤頁面的代碼。我已粘貼完整的代碼以防萬一。 – Hoff 2013-02-21 15:45:20

+0

好吧我不知道爲什麼,但如果我把'e.Result = result.Skip(0)'它的工作原理應該如此,分頁和所有。問題解決了,感謝您的幫助! – Hoff 2013-02-21 16:32:23