代码之家  ›  专栏  ›  技术社区  ›  Dmitris

问题:无法加载视图状态

  •  2
  • Dmitris  · 技术社区  · 17 年前

    我正在为我的大学项目制作一个网站。 这个项目是从Web服务获取一切的网站。 有人能告诉我发生了什么事,我是怎么解决的吗?

    在我的一个页面上,我有ListView控件来显示产品项,在同一个页面上有寻呼机。 在第一次页面上,渲染效果很好,所有内容都会显示出来。当我单击Nextpage on 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();
        }
    

    在Web服务上:

    [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之外,那么我不会出错,但是如果我单击NextPage链接,它会显示相同的数据集。

    我该怎么修? 提前谢谢。

    3 回复  |  直到 17 年前
        1
  •  3
  •   Ofer    16 年前

    在您的代码后面添加:

    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
  •  2
  •   Jay    17 年前

    这不完全是我的专长,但我可以提供一些概念指导。

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

    您的服务将需要接受索引和长度参数,这样浏览器就可以说“我现在在项目20上,一次查看10个,所以现在转到数据库并返回项目30-39。”

    您的服务实际上必须支持分页。或者,您可以将所有项目一次性加载到页面中,并使用jquery(如库)进行“虚拟”分页,而不是回发,但我认为这超出了学校项目的范围。

        3
  •  0
  •   Dmitris    17 年前

    杰,谢谢你的帮助。

    在我的头撞到墙上一段时间后,我终于为我的ListView定制了寻呼机。 现在一切都正常了。

    推荐文章