Asp.net C# listview paging Failed to load viewstate -
i have problem paging using datapager on list view. page simple. enter search text. click on search button. display result list view. result display fine every time click on page number got following error:
server error in '/' application. failed load viewstate. control tree viewstate being loaded must match control tree used save viewstate during previous request. example, when adding controls dynamically, controls added during post-back must match type , position of controls added during initial request. description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.web.httpexception: failed load viewstate. control tree viewstate being loaded must match control tree used save viewstate during previous request. example, when adding controls dynamically, controls added during post-back must match type , position of controls added during initial request. source error: unhandled exception generated during execution of current web request. information regarding origin , location of exception can identified using exception stack trace below. stack trace: [httpexception (0x80004005): failed load viewstate. control tree viewstate being loaded must match control tree used save viewstate during previous request. example, when adding controls dynamically, controls added during post-back must match type , position of controls added during initial request.] system.web.ui.control.loadviewstaterecursive(object savedstate) +317 system.web.ui.control.loadchildviewstatebyindex(arraylist childstate) +144 system.web.ui.control.loadviewstaterecursive(object savedstate) +204 system.web.ui.control.loadchildviewstatebyindex(arraylist childstate) +144 system.web.ui.control.loadviewstaterecursive(object savedstate) +204 system.web.ui.control.loadchildviewstatebyindex(arraylist childstate) +144 system.web.ui.control.loadviewstaterecursive(object savedstate) +204 system.web.ui.control.loadchildviewstatebyindex(arraylist childstate) +144 system.web.ui.control.loadviewstaterecursive(object savedstate) +204 system.web.ui.control.loadchildviewstatebyindex(arraylist childstate) +144 system.web.ui.control.loadviewstaterecursive(object savedstate) +204 system.web.ui.control.loadchildviewstatebyindex(arraylist childstate) +144 system.web.ui.control.loadviewstaterecursive(object savedstate) +204 system.web.ui.control.loadchildviewstatebyindex(arraylist childstate) +144 system.web.ui.control.loadviewstaterecursive(object savedstate) +204 system.web.ui.page.loadallstate() +464 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +1849 version information: microsoft .net framework version:4.0.30319; asp.net version:4.0.30319.34212
here makeup:
<%@ page title="" language="c#" masterpagefile="~/site.master" autoeventwireup="true" codebehind="search.aspx.cs" inherits="itdb.views.employee.search"%> <asp:content id="content1" contentplaceholderid="header" runat="server"> </asp:content> <asp:content id="content2" contentplaceholderid="maincontent" runat="server"> <br /> <asp:textbox id="searchtext" runat="server"></asp:textbox> <asp:button id="cmdsearch" runat="server" text="search" onclick="cmdsearch_click" /> <hr /> <asp:listview id="searchlist" runat="server" datakeynames="employeeid" itemtype="itdb.dbcontext.employee" > <emptydatatemplate> there no entries found employee </emptydatatemplate> <layouttemplate> <asp:placeholder runat="server" id="itemplaceholder" /> <asp:datapager pagesize="5" runat="server"> <fields> <asp:nextpreviouspagerfield showlastpagebutton="false" shownextpagebutton="false" buttontype="button" buttoncssclass="btn" /> <asp:numericpagerfield buttontype="button" numericbuttoncssclass="btn" currentpagelabelcssclass="btn disabled" nextpreviousbuttoncssclass="btn" /> <asp:nextpreviouspagerfield showfirstpagebutton="false" showpreviouspagebutton="false" buttontype="button" buttoncssclass="btn" /> </fields> </asp:datapager> </layouttemplate> <itemtemplate> <%#: item.firstname + " " + item.lastname%> <br /> </itemtemplate> </asp:listview> </asp:content> <asp:content id="content3" contentplaceholderid="afterform" runat="server"> </asp:content>
code behind:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.entity; using itdb.dbcontext; namespace itdb.views.employee { public partial class search : system.web.ui.page { protected itdb.dbcontext.itdb_mssql_connection _db = new itdb.dbcontext.itdb_mssql_connection(); protected void page_load(object sender, eventargs e) { } protected void cmdsearch_click(object sender, eventargs e) { string szsearch = searchtext.text; searchlist.datasource = getsearchlist(szsearch); searchlist.databind(); } private list<itdb.dbcontext.employee> getsearchlist(string searchstring) { return (from employee in _db.employee employee.firstname.contains(searchstring) || employee.lastname.contains(searchstring) orderby employee.lastname select employee ).tolist(); } } }
what doing wrong here? lot help.
a answer here, since fields not know until run time.
http://blog.yeshere.org/2011/04/using-datapager-in-listview.html
set datapager's id to: datapager1
you should add , implement pagepropertieschanging event of listview. pagepropertieschangingeventargs event argument provide needy paging properties (startrowindex , maximumrows) can supply them datapager.
if datapager placed inside listview, yours this:
protected void listview1_pagepropertieschanging(object sender, pagepropertieschangingeventargs e) { listview lv = sender listview; datapager pager = lv.findcontrol("datapager1") datapager; pager.setpageproperties(e.startrowindex, e.maximumrows, false); binddata(); // set datasource listview , call databind() of listview }
if outside:
protected void listview1_pagepropertieschanging(object sender, pagepropertieschangingeventargs e) { this.datapage1.setpageproperties(e.startrowindex, e.maximumrows, false); binddata(); // set datasource listview , call databind() of listview }
Comments
Post a Comment