c# - 问题: Failed to load viewstate

标签 c# .net asp.net linq web-services

我正在为我的大学项目制作一个网站。 该项目是一个从网络服务获取一切的网站。 有人可以告诉我发生了什么事以及我如何解决它吗?

在我的一个页面上,我有 ListView 控件来显示产品项目,并且在同一页面上有 Pager。 第一次页面渲染良好并且所有内容都显示出来。当我点击寻呼机上的下一页时,它会重新加载页面,但不显示第二页,而是显示相同的数据,如果我再次按下,我会得到:

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: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> 

在后面的代码中我有:

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

在网络服务上:

[WebMethod]
    public List<ws_product> getProductList()
    {
        using (DataClassesDataContext myProducts = new DataClassesDataContext())
        {

            var productList = from p in myProducts.ws_products
                              select p;

            return productList.ToList();
        }

}

有人能告诉我发生了什么事吗? 另外,如果我在 ListView 之外使用 Pager,那么我不会收到错误,但是如果我单击下一页链接,它会显示相同的数据集。

我该如何修复它? 预先感谢您。

最佳答案

在您的代码后面添加:

protected void lds_Selecting(object sender, LinqDataSourceSelectEventArgs args)
{
        Store.Service myProducts = new Store.Service();  
        args.Result  = myProducts.getProductList();     
}

将 asp:LinqDataSource 添加到页面:

  <asp:LinqDataSource ID="lds" runat="server" OnSelecting="lds_Selecting" />


    <asp:ListView ID="lv_ProductList" runat="server" 
DataKeyNames="product_id"   DataSourceID="lds">   
            <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> 

这应该有效。

关于c# - 问题: Failed to load viewstate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/931116/

相关文章:

c# - 通过子项无限嵌套 ngFor

c# - 如何对我的 WCF 客户端类进行单元测试?

asp.net - 使用 CORS 避免预检 OPTIONS 请求

c# - 如何在代码隐藏文件中的 asp.net 表中插入 <a href> 标记?

c# - 生成datalist次数

c# - 如何跟踪DataGrid WPF MVVM的更改

c# - 进程需要按 <enter> 才能继续。可以使用 Mono GTK# 吗?

.net - 使用 .NET 服务引用调用 Web 服务的开销是多少?

c# - Visual Studio 2013 不发现单元测试

c# - 使用 Specflow 和 NUnit 版本 3 并行运行测试