locked
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 RRS feed

  • Question

  • Generally I had bind the data to gridview using template fields in asp.net.in this i have use dropdownlist in edittemplate.while the application is in excution mode click edit button at the time application raise the 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, " 

    Default.aspx

    <%@ Page Language="C#"  AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
     
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
       <title>/title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <br />
            <br />
            <br />
            <asp:GridView ID="GridView1"    AutoGenerateColumns="False" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" runat="server" style="    height: 139px; width: 187px" OnRowEditing="GridView1_RowEditing">
                Columns>
                    <asp:BoundField HeaderText="empid" DataField="empid" />
                    <asp:BoundField HeaderText="empname" DataField="empname" />
                    <asp:BoundField HeaderText="depid" DataField="depid" />               
                    <asp:TemplateField HeaderText="Gender">
                        <EditItemTemplate>
                            <asp:DropDownList ID="Fropdownlist1" runat="server" SelectedValue='&lt;%# Bind("Gender") %>'>
                                <asp:ListItem>Select Gender &lt;/asp:ListItem>
                                <asp:ListItem>Male &lt;/asp:ListItem>
                                <asp:ListItem>Female &lt;/asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("Gender") %>'>&lt;/asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <br />
            <br />
            <br />
        
        </div>
        </form>
    </body>
    </html>

    Default.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
     
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=AMMA-PC\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Page.IsPostBack==false)
            {
                Getdata();
            }
        }
        public void Getdata()   
        {
            SqlDataAdapter da=new SqlDataAdapter("select * from emp",con);
            DataSet ds=new DataSet();
            da.Fill(ds);
            GridView1.DataSource=ds;
            GridView1.DataBind();
        }
     
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
        }
    }

    please help me,
    Thank u.




    • Edited by ramesh rammi Friday, December 1, 2017 6:39 AM
    • Moved by CoolDadTx Monday, December 4, 2017 3:04 PM ASP.NET related
    Friday, December 1, 2017 6:30 AM

All replies

  • Hi ramesh rammi,

    Please set enableviewstate=false on the control if you don't need the viewstate and check if it works for you.

    In addition, since the issue is more related to asp.net, I would suggest that you could post your issue on asp.net forum for suitable support.

    https://forums.asp.net/18.aspx/1?Web+Forms

    Thanks for your understanding.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 4, 2017 2:06 AM