在网格视图中将下拉菜单置于编辑模式时遇到问题-无法加载viewstate [英] having trouble put dropdown in edit mode in grid view - failed to load viewstate

查看:153
本文介绍了在网格视图中将下拉菜单置于编辑模式时遇到问题-无法加载viewstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图在gridview编辑模式下而不是标签中填充下拉列表,但是我一直收到此错误,因为我发现它是因为我将DDL代替了标签,但是每个人都做了同样的事情,对他们来说都没有问题,请帮忙我来找出为什么我一直收到此错误以及如何解决它,谢谢.

错误:
无法加载viewstate.加载视图状态的控制树必须与在上一个请求期间用于保存视图状态的控制树匹配.例如,当动态添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配.


这是我的网格视图代码:

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



我尝试了两种方法,一种直接起作用,也使用gridview数据绑定,但是都得到相同的错误:



I have tried two approach one direct to function and also use gridview databound but both get the same error:

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


             }
              }


     }


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



  }

推荐答案

将DDL行更改为:
change the DDL line to :
<asp:dropdownlist id="ddlShippingvia" runat="server" enableviewstate="false" xmlns:asp="#unknown" />



但仍然有问题,不会填充



But still have problem It wont populate


已解决
甚至只需要在编辑中调用gridview

solved already
just need to recall the gridview in edit even

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

     }


这篇关于在网格视图中将下拉菜单置于编辑模式时遇到问题-无法加载viewstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆