Sunday, 15 January 2012

c# - Custom Paging With LinqDataSource & Formview -



c# - Custom Paging With LinqDataSource & Formview -

i trying to utilize linqdatasource's selecting event filter info in formview, can't seem page properly. can manage 1 record show in formview paging controls not show up. have next code in linqdatasource selecting event:

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);

as mentioned above, code works 1 record displayed , paging controls don't show on formview. have tried modify e.result following, object reference not set instance of object. exception:

e.result = result;

what right way page formview using linqdatasource's selecting event?

edit 1

as requested, here formview , linqdatasource's markup:

<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 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 lastly 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 division, 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 department, 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>

from msdn can see autopage property of linqdatasource set true default. docs:

when autopage property set true, linqdatasource command retrieves plenty records 1 page in data-bound control. uses skip<tsource> , take<tsource> methods retrieve records current page.

the consequence of need alter few things paging working.

currently setting e.arguments.startrowindex = 0;. don't need everytime interfere paging. doing e.result = result.skip(fvmain.pageindex).take(1); not necessary either. e.result = result; should fine.

i think combination of 2 things causing issue. however, need handle case 1 of filters kicks in.

the first problem need persist applied filter in viewstate, otherwise unable tell when filter has been applied , when existing one.

inspired after applying filter linqdatasource connected gridview ,clicking edit bring me old data, add together property page (you need 1 lastly name):

public string firstnamefilter { { homecoming (string)this.viewstate["firstnamefilter"] ?? string.empty; } set { this.viewstate["firstnamefilter"] = value; } }

then in selecting event handler, run code:

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;

c# formview linqdatasource

No comments:

Post a Comment