Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
I am trying to populate the dropdownlist in gridview edit mode instead of label but I keep getting this error , as I found out its because I am putting DDL instead of label but everyone did the same thing and no problem at all for them , please help me to find why I keep getting this error and how to fix it , thanks.

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


here is my code for grid view :
ASP.NET
        <asp:GridView ID="smOrderShipmentList" runat="server" 
                        AutoGenerateColumns="False" Width="100%" BackColor="LightGoldenrodYellow" 
                        BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" 
                        GridLines="None" AllowPaging="false" 
                        OnRowDataBound="smOrderShipmentList_RowDataBound" 
            DataKeyNames="OrderShippingID" OnRowEditing="smOrderShipment_RowEditing" 
            onrowupdating="smOrderShipmentList_RowUpdating" 
            onrowcancelingedit="smOrderShipmentList_RowCancelingEdit">
                    <Columns>
                    
                        <asp:TemplateField HeaderText="Order Shipping ID" ItemStyle-Width="10%">
                            <ItemTemplate>
                            
                              <a   href="java<!-- no -->script:collapseExpand('orderShippingID2-<%# Eval("OrderShippingID") %>');"> <asp:Label id="lblOrderShippingID" runat="server" Text='<%# Eval("OrderShippingID")%>' /></a>
                            
                            </ItemTemplate>
                        </asp:TemplateField>
                       <%-- <asp:BoundField DataField="ShippingVia" HeaderText="Shipping Via" ItemStyle-Width="12%"/>--%>
                        <asp:TemplateField HeaderText="Shipping Via" ItemStyle-Width="12%" >
                        <ItemTemplate>
                        <%--  <asp:DropDownList ID="lblShippingvia" runat="server" ><asp:ListItem>default</asp:ListItem>
                          </asp:DropDownList>--%>
                        <asp:Label ID="lblShippingvia" runat="server" Text='<%# Eval("ShippingVia") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:DropDownList ID="txtShippingvia" runat="server" /><%--DataSource='<%# bindshippmethods(Eval("SellerID"))%>' AppendDataBoundItems="True" />--%>
                        </EditItemTemplate>
                        </asp:TemplateField>
...


I have tried two approach one direct to function and also use gridview databound but both get the same error:
C#
protected void smOrderShipmentList_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              int orderShippingID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "OrderShippingID"));

              GridView gvChild = (GridView)e.Row.FindControl("gvOrderShippingDetail");
              var orderShippingDetailList = Shipping.GetOrderShippingDetailList("", orderShippingID, -1, true);
              gvChild.DataSource = orderShippingDetailList;
              gvChild.DataBind();
          }



         if (e.Row.RowType == DataControlRowType.DataRow) {
             if ((e.Row.RowState & DataControlRowState.Edit) > 0)
             {
                 DropDownList ddl = (DropDownList)e.Row.FindControl("txtShippingvia");


                     ddl.DataSource = bindshippmethods(DataBinder.Eval(e.Row.DataItem, "lblSellerID"));
                     ddl.DataValueField = "shippingID";
                     ddl.DataTextField = "ship";
                     ddl.DataBind();
                     ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "lblShippingvia").ToString();


             }
              }


     }

C#
protected List<string> bindshippmethods(object sellerid)
      {
          List<string> shmp = new List<string>();
          foreach (var item in  Shipping.GetAllShippingMethodBySellerId(sellerid.ToString()))
                  {
                      shmp.Add(item.ToString());

                      }

          return shmp;
      }



  }
Posted
Comments
Malay Kumar Pandey 28-May-14 23:39pm    
Use this post :
http://stackoverflow.com/questions/833490/gridview-row-editing-dynamic-binding-to-a-dropdownlist
Shadow codder 29-May-14 0:14am    
I have done all those ways , I keep getting this error :
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.
and its only go a ways when I put both template mode and edit mode as DDL but I want to put template more Label and edit mode DDL

change the DDL line to :
C#
<asp:dropdownlist id="ddlShippingvia" runat="server" enableviewstate="false" xmlns:asp="#unknown" />


But still have problem It wont populate
 
Share this answer
 
Comments
Shadow codder 29-May-14 3:22am    
still wont go to the function from row data bound means it wont pass through the if condition for edit
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
solved already
just need to recall the gridview in edit even

protected void smOrderShipmentList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
     {
         smOrderShipmentList.EditIndex =-1;
         LoadData();

     }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900