【问题标题】:Problem: Failed to load viewstate问题:无法加载视图状态
【发布时间】:2009-05-31 02:26:59
【问题描述】:

我正在为我的大学项目制作一个网站。 该项目是从 Web 服务获取一切的网站。 谁能告诉我发生了什么,以及我如何解决它?

在我的一个页面上,我有 ListView 控件来显示产品项目,而 Pager 在同一页面上。 在第一次页面呈现良好并显示所有内容。当我点击 Pager 上的 nextPage 时,它​​会重新加载页面但不显示第二页,而是显示相同的数据,如果我再推一次,那么我就开始了:

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,则不会出现错误,但是如果我单击 nextpage 链接,它会显示相同的数据集。

我该如何解决? 提前谢谢你。

【问题讨论】:

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


    【解决方案1】:

    在你的代码后面添加:

    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> 
    

    这应该可以。

    【讨论】:

      【解决方案2】:

      这并不完全是我的专业领域,但我或许能够提供一些概念性指导。

      虽然您在页面上放置了分页器控件,但您还没有在代码隐藏或服务中实现分页。

      您的服务将需要接受索引和长度参数,因此浏览器可以说“我现在在第 20 项上,我一次在看 10 项,所以现在去数据库并发回项目30-39。”

      您的服务必须真正支持分页。或者,您可以一次将所有项目加载到页面中,然后使用类似 jQuery 的库进行“虚拟”分页,而不是回发,但我想这超出了您学校项目的范围。

      【讨论】:

        【解决方案3】:

        Jay,感谢您的帮助。

        在墙上折断了一段时间后,我最终为我的 ListView 构建了自定义寻呼机。 现在一切正常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多