I am making a web site for my college project. The project is website thats gets everything from web service. Can somebody tell whats going on, and how i fix it ?
On one of my pages i have ListView control to display product items, and Pager on same page. On the first time page renderes fine and everything is displayed. When i click on nextPage on Pager, its reload page but doesn't display second page, rather displays same data, if i push once more then i am gettin:
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.
My products page i quiet simple:
<asp:ListView ID="lv_ProductList" runat="server" DataKeyNames="product_id">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
<br />
<br />
<asp:DataPager ID="pageTopics" runat="server" PageSize="8" PagedControlID="lv_ProductList" >
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" ButtonCssClass="span" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="true" ShowLastPageButton="false" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<div class="item">
<img src="images/pic_1.jpg" width="91" height="105" alt="iPod" class="left" />
<h3><a href="http://www.justwebtemplates.com"><%# Eval("product_name") %></a></h3>
<p><%# Eval("product_desc").ToString().Substring(0,90) + "..." %> </p>
<div><a href="http://www.freewebsitetemplates.com" class="details">details</a> <a href="http://www.freewebsitetemplates.com" class="addtocart">add to cart</a></div>
<div class="divider"></div>
</div>
</ItemTemplate>
<EmptyDataTemplate>No products found</EmptyDataTemplate>
<EmptyItemTemplate>No products found</EmptyItemTemplate>
</asp:ListView>
And in the code behind i have:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getProductsList();
}
}
public void getProductsList()
{
Store.Service myProducts = new Store.Service();
var products = myProducts.getProductList();
lv_ProductList.DataSource = products;
lv_ProductList.DataBind();
}
and on the web service:
[WebMethod]
public List<ws_product> getProductList()
{
using (DataClassesDataContext myProducts = new DataClassesDataContext())
{
var productList = from p in myProducts.ws_products
select p;
return productList.ToList();
}
}
Can somebody tell whats going on ? Also if i take Pager outside the ListView, then i don't get error, however if i click on the nextpage link it displays same set of data.
How do i fix it ? Thank you in advance.