Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error-- when i click on Previous button after click on Next button ,or when i click on last button this error message i get and in ever scenario it doesnot Page my List of records--


Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

ASP.NET
 <asp:ListView ID="ListJobs" runat="server">
   <LayoutTemplate>
      <table style="border: solid 2px #336699; width:800px; " cellspacing="0" cellpadding="3" rules="all">
         <tr style="background-color: #336699; color: White;">
            <th> Posted Date </th>
            <th> Jobtitle </th>
            <th> Location </th>
         </tr>        
         
         <tr>
            <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
         </tr>
         
                  
         <tr id="Tr2"  runat="server" class="TablePager">  
                        <td id="Td4"  runat="server" colspan="3">  
                            <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListJobs" PageSize="3">  
                                <Fields>  
                                    <asp:NextPreviousPagerField   
                                        ButtonType="Button"   
                                        ShowFirstPageButton="true"  
                                        ShowNextPageButton="true"  
                                        ShowPreviousPageButton="false"  
                                        ButtonCssClass="ButtonCSS"  
                                        />  
                                    <asp:NumericPagerField   
                                        NumericButtonCssClass="NumericButtonCSS"  
                                        CurrentPageLabelCssClass="CurrentPageLabelCSS"  
                                        NextPreviousButtonCssClass="NextPreviousButtonCSS"  
                                        />  
                                    <asp:NextPreviousPagerField   
                                        ButtonType="Button"  
                                        ShowLastPageButton="true"  
                                        ShowNextPageButton="false"  
                                        ButtonCssClass="ButtonCSS"  
                                        />  
                                </Fields>  
                            </asp:DataPager>  
                        </td>  
                    </tr>         
        
      </table>  
   </LayoutTemplate>
   <ItemTemplate>
      <tr>
         <td   style=" width:150px;" ><%# Eval("CreatedDate")%></td>
         <td  style=" width:500px;" > <a href='<%#"JobDetail.aspx?Name="+Eval("Job_Id")%>'</a><%# Eval("JobTitle")%></td>
         <td  style=" width:150px;"><%# Eval("Location")%></td>
      </tr>
   </ItemTemplate>
   <AlternatingItemTemplate>
      <tr style="background-color: #dadada;">         
         <td   style=" width:150px;" ><%# Eval("CreatedDate")%></td>
         <td  style=" width:500px;" > <a href='<%#"JobDetail.aspx?Name="+Eval("Job_Id")%>'</a><%# Eval("JobTitle")%></td>
         <td  style=" width:150px;"><%# Eval("Location")%></td>
      </tr>
   </AlternatingItemTemplate>
   
   <EmptyDataTemplate>
       No Record Found 
   </EmptyDataTemplate>
</asp:ListView>
Posted
Updated 14-Mar-13 4:23am
v4

Use DataPager like this article Effective paging with List View control in ASP.NET[^]
 
Share this answer
 
Comments
sr_24 14-Mar-13 9:16am    
thanks for u reply but ..have u find any thing wrong in my cod e please have a look..pls
The problem is due to the binding occuring on the Page_Load event.

For this to work as expected, the binding needs to happen in the DataPager's OnPreRender event, not in the Page_Load.

Source:
<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
    OnPreRender="ListPager_PreRender">

<Fields>
        <asp:NumericPagerField  />
    </Fields>
</asp:DataPager>



Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
    //Binding code moved from Page_Load
    //to the ListView's PreRender event
}

protected void ListPager_PreRender(object sender, EventArgs e)
{
    MyList.DataSource = GetSomeList();
    MyList.DataBind();
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900