locked
C# listview table cannot navigate to another page due to an error RRS feed

  • Question

  • User766369706 posted

    Hi, I am currently facing a problem where i press one of the ListView item and the error:
    "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phase" appears. May i know how to solve such issue? Below are my codes

    <div class="shoppingList">
    <asp:ListView runat="server" ID="ListView1" GroupItemCount="5" OnPagePropertiesChanging="ListView1_PagePropertiesChanging">
    <LayoutTemplate>
    <table cellpadding="4" runat="server" id="tblProducts" style="height: 320px">
    <tr runat="server" id="groupPlaceholder">
    </tr>
    </table>
    <div class="pagerContent">
    <asp:DataPager runat="server" ID="DataPager" PageSize="10" OnPreRender="DataPagerProducts_PreRender">
    <Fields>
    <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="true" ShowPreviousPageButton="true" ShowNextPageButton="false" />
    <asp:NumericPagerField ButtonType="Link" NumericButtonCssClass="btn" CurrentPageLabelCssClass="btn3" ButtonCount="3" />
    <asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="true" ShowNextPageButton="true" ShowPreviousPageButton="false" />
    </Fields>
    </asp:DataPager>
    </div>
    
    </LayoutTemplate>
    <GroupTemplate>
    <tr runat="server" id="productRow"
    style="height: 80px">
    <td runat="server" id="itemPlaceholder"></td>
    </tr>
    </GroupTemplate>
    <ItemTemplate>
    <td valign="top" align="center" style="width: 250px" runat="server">
    <asp:ImageButton ID="ProductImage" runat="server"
    ImageUrl='<%# Eval("ProductImage") %>'
    Height="250px" />
    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" Text='<% #Eval("ProductName")%>'
    NavigateUrl='<%#"~/ShoppingProduct.aspx?ProductID="+ Eval("ProductID")%>' />
    </td>
    </ItemTemplate>
    </asp:ListView>
    

    Here the method

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindData();
    }
    }
    
    protected void BindData()
    {
    ListView1.DataSource = sBLL.retrieveAllProudctAvailable();
    ListView1.DataBind();
    }
    protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
    ListView1.DataSource = sBLL.retrieveAllProudctAvailable();
    ListView1.DataBind();
    }
    
    protected void DataPagerProducts_PreRender(object sender, System.EventArgs e)
    {
    ListView1.DataBind(); <-- This where i got the error that I indicate above
    base.OnPreRender(e);
    }

    Wednesday, June 29, 2016 11:42 AM

Answers

  • User-271186128 posted

    Hi Alvin096,

    Welcome to asp.net forum.

    "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phase" appears. May i know how to solve such issue? Below are my codes

    protected void DataPagerProducts_PreRender(object sender, System.EventArgs e)
    {
    ListView1.DataBind(); <-- This where i got the error that I indicate above
    base.OnPreRender(e);
    }

    I find you don't provide a datasource for the listview to bind data.

    So, I suggest you change the code as follow:

    protected void DataPagerProducts_PreRender(object sender, System.EventArgs e)
    {
    ListView1.DataSource = dt();
    ListView1.DataBind(); //<-- This where i got the error that I indicate above
    base.OnPreRender(e);
    }
    

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 30, 2016 6:58 AM