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='<%# Bind("Gender") %>'>
<asp:ListItem>Select Gender </asp:ListItem>
<asp:ListItem>Male </asp:ListItem>
<asp:ListItem>Female </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'></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.