未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。我想用GridView1和DetailsView1实现商品列表以及具体商品的编辑 删除功能,在实现了一次编辑后,再次点击某个商品后出现了以上错误提示
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="商品管理2.aspx.cs" Inherits="汇通玩具网站.admin.商城.商品管理2" %>
<%@ Register Assembly="CuteEditor" Namespace="CuteEditor" TagPrefix="CE" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>商品管理</title>
</head>
<body>
    <form id="form1" runat="server">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:汇通玩具网站ConnectionString %>" 
                SelectCommand="SELECT 商品.商品编号, 商品.商品名称, 商品.商品规格, 商品.商品面料, 商品.商品A价, 商品.商品B价, 商品.是否精品, 商品.是否特价商品, 商品.商品库存, 商品类别.商品类别名称, 商品.商品小图地址, 商品.商品大图地址, 商品.商品中图地址, 商品.商品添加时间, 商品.商品添加IP FROM 商品 INNER JOIN 商品类别 ON 商品.商品类别编号 = 商品类别.商品类别编号 order by 商品.商品添加时间 desc">
            </asp:SqlDataSource>
            <br />
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="商品编号" 
                DataSourceID="SqlDataSource1" onrowediting="GridView1_RowEditing" 
                onselectedindexchanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:CommandField SelectText="查看详细信息" ShowSelectButton="True" />
                    <asp:BoundField DataField="商品编号" HeaderText="商品编号" InsertVisible="False" 
                        ReadOnly="True" SortExpression="商品编号" />
                    <asp:ImageField DataImageUrlField="商品小图地址" HeaderText="商品小图">
                        <ControlStyle Height="100px" Width="100px" />
                    </asp:ImageField>
                    <asp:BoundField DataField="商品名称" HeaderText="商品名称" SortExpression="商品名称" />
                    <asp:BoundField DataField="商品类别名称" HeaderText="商品类别名称" 
                        SortExpression="商品类别名称" />
                    <asp:BoundField DataField="商品规格" HeaderText="商品规格" SortExpression="商品规格" />
                    <asp:BoundField DataField="商品面料" HeaderText="商品面料" SortExpression="商品面料" />
                    <asp:BoundField DataField="商品A价" HeaderText="商品A价" SortExpression="商品A价" />
                    <asp:BoundField DataField="商品B价" HeaderText="商品B价" SortExpression="商品B价" />
                    <asp:CheckBoxField DataField="是否精品" HeaderText="是否精品" SortExpression="是否精品" />
                    <asp:CheckBoxField DataField="是否特价商品" HeaderText="是否特价商品" 
                        SortExpression="是否特价商品" />
                    <asp:BoundField DataField="商品库存" HeaderText="商品库存" SortExpression="商品库存" />
                    <asp:BoundField DataField="商品添加时间" HeaderText="商品添加时间" 
                        SortExpression="商品添加时间" />
                    <asp:BoundField DataField="商品添加IP" HeaderText="商品添加IP" 
                        SortExpression="商品添加IP" />
                </Columns>
                <EmptyDataTemplate>
                    暂时没有商品
                </EmptyDataTemplate>
            </asp:GridView>
    <div>    
        <br />
                <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataKeyNames="商品编号" DataSourceID="SqlDataSource2" Height="50px" 
            Width="1047px" onitemupdating="DetailsView1_ItemUpdating" 
                    onitemdeleted="DetailsView1_ItemDeleted" 
                    onitemupdated="DetailsView1_ItemUpdated">
                    <CommandRowStyle Width="30%" />
                    <Fields>
                        <asp:BoundField DataField="商品编号" HeaderText="商品编号" InsertVisible="False" 
                    ReadOnly="True" SortExpression="商品编号" />
                        <asp:BoundField DataField="商品名称" HeaderText="商品名称" SortExpression="商品名称" />
                        <asp:TemplateField HeaderText="商品小图">
                            <EditItemTemplate>
                                <asp:FileUpload ID="商品小图上传" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Image ID="商品小图" ImageUrl='<%#Bind("商品小图地址")%>' Width="100px" 
                            Height="100px" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="商品大图">
                            <EditItemTemplate>
                                <asp:FileUpload ID="商品大图上传" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Image ID="商品大图" ImageUrl='<%#Bind("商品大图地址")%>' Width="100px" 
                            Height="100px" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="商品中图">
                            <EditItemTemplate>
                                <asp:FileUpload ID="商品中图上传" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Image ID="商品中图" ImageUrl='<%#Bind("商品中图地址")%>' Width="100px" 
                            Height="100px" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="商品规格" HeaderText="商品规格" SortExpression="商品规格" />
                        <asp:BoundField DataField="商品面料" HeaderText="商品面料" SortExpression="商品面料" />
                        <asp:BoundField DataField="商品A价" HeaderText="商品A价" SortExpression="商品A价" />
                        <asp:BoundField DataField="商品B价" HeaderText="商品B价" SortExpression="商品B价" />
                        <asp:TemplateField HeaderText="商品类别">
                            <EditItemTemplate>
                                <asp:DropDownList ID="商品分类" runat="server" DataSourceID="SqlDataSource3" 
                            SelectedValue='<%# Bind("商品类别编号") %>' DataTextField="商品类别名称" 
                            DataValueField="商品类别编号">
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="商品类别名称" runat="server" Text='<%#Bind("商品类别名称")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>

解决方案 »

  1.   

                            <asp:TemplateField HeaderText="商品添加时间">
                                <EditItemTemplate>
                                    <asp:TextBox ID="商品添加时间" Text='<%#DateTime.Now%> ' ReadOnly="true" 
                                  runat="server"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="商品添加时间" runat="server" Text='<%#Bind("商品添加时间")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="商品添加IP" HeaderText="商品添加IP" 
                        SortExpression="商品添加IP" Visible="false" />
                            <asp:CheckBoxField DataField="是否精品" HeaderText="是否精品" SortExpression="是否精品" />
                            <asp:TemplateField HeaderText="商品详细说明">
                                <EditItemTemplate>
                                    <CE:Editor ID="商品详细说明" AutoConfigure="Simple" Text='<%#Bind("商品详细说明")%>' 
                                runat="server">
                                    </CE:Editor>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="商品详细说明" runat="server" Text='<%#Bind("商品详细说明")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CheckBoxField DataField="是否特价商品" HeaderText="是否特价商品" 
                        SortExpression="是否特价商品" />
                            <asp:BoundField DataField="商品库存" HeaderText="商品库存" SortExpression="商品库存" />
                            <asp:CommandField ButtonType="Button" HeaderText="编辑、更新" ShowEditButton="True" 
                        ShowHeader="True" ShowDeleteButton="True" />
                        </Fields>
                    </asp:DetailsView>        <br />
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:汇通玩具网站ConnectionString %>" 
                SelectCommand="SELECT DISTINCT * FROM [商品] inner JOIN [商品类别] ON 商品.商品类别编号=商品类别.商品类别编号 WHERE ([商品编号] = @商品编号) order by 商品.商品添加时间 desc " 
                DeleteCommand="删除商品存储过程" DeleteCommandType="StoredProcedure" 
                UpdateCommand="修改商品存储过程" UpdateCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:ControlParameter ControlID="GridView1" Name="商品编号" 
                        PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
                <DeleteParameters>
                    <asp:ControlParameter ControlID="GridView1" Name="商品编号" 
                        PropertyName="SelectedValue" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:ControlParameter ControlID="GridView1" Name="商品编号" 
                        PropertyName="SelectedValue" Type="Int32" />
                    <asp:Parameter Name="商品名称" Type="String" />
                    <asp:Parameter Name="商品小图地址" Type="String" DefaultValue="" />
                    <asp:Parameter Name="商品大图地址" Type="String" DefaultValue="" />
                    <asp:Parameter Name="商品中图地址" Type="String" DefaultValue="" />
                    <asp:Parameter Name="商品规格" Type="String" />
                    <asp:Parameter Name="商品面料" Type="String" />
                    <asp:Parameter Name="商品A价" Type="Decimal" />
                    <asp:Parameter Name="商品B价" Type="Decimal" />
                    <asp:Parameter Name="商品类别编号" Type="Int32" />
                    <asp:Parameter Name="是否精品" Type="Boolean" />
                    <asp:Parameter Name="商品详细说明" Type="String" />
                    <asp:Parameter Name="是否特价商品" Type="Boolean" />
                    <asp:Parameter Name="商品库存" Type="Int32" />
                    <asp:Parameter Name="商品添加时间" Type="DateTime" />
                    <asp:Parameter Name="商品添加IP" Type="String" />
                </UpdateParameters>
            </asp:SqlDataSource>
            <br />
            <br />
                <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:汇通玩具网站ConnectionString %>" 
            SelectCommand="返回具有层次的商品类别" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
        </div>
        </form>
    </body>
    </html>
      

  2.   

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using CuteEditor;
    using CuteEditor.ImageEditor;
    using CuteEditorDemo;
    using NetSpell.SpellChecker;
    using 查询类;namespace 汇通玩具网站.admin.商城
    {
        public partial class 商品管理2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                
            }        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridView1.Visible = false;
                DetailsView1.Visible = true;
            }        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                
            }        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
            {
                //SqlDataSource2.UpdateParameters.Clear();            FileUpload 商品小图 = (FileUpload)DetailsView1.FindControl("商品小图上传");            if (商品小图.HasFile)
                {
                    string datetime = DateTime.Now.Date.ToString("yyyyMMdd");
                    string time = DateTime.Now.ToShortTimeString().Replace(":", "");
                    string 新文件名 = datetime + time + DateTime.Now.Millisecond.ToString();
                    string 文件名 = 商品小图.FileName;
                    string 文件大小 = 商品小图.PostedFile.ContentLength.ToString();
                    string 文件名后缀 = 文件名.Substring(文件名.LastIndexOf(".") + 1).ToLower();                string 小图上传完整路径 = Server.MapPath("上传图片/小图/") + 新文件名 + @"." + 文件名后缀;
                    商品小图.SaveAs(小图上传完整路径);
                    SqlDataSource2.UpdateParameters["商品小图地址"].DefaultValue = "上传图片/小图/" + 新文件名 + @"." + 文件名后缀;            }
                else
                {
                    
                    SqlDataSource2.UpdateParameters["商品小图地址"].DefaultValue = "11";
                }            FileUpload 商品大图 = (FileUpload)DetailsView1.FindControl("商品大图上传");            if (商品大图.HasFile)
                {
                    string datetime = DateTime.Now.Date.ToString("yyyyMMdd");
                    string time = DateTime.Now.ToShortTimeString().Replace(":", "");
                    string 新文件名 = datetime + time + DateTime.Now.Millisecond.ToString();
                    string 文件名 = 商品大图.FileName;
                    string 文件大小 = 商品大图.PostedFile.ContentLength.ToString();
                    string 文件名后缀 = 文件名.Substring(文件名.LastIndexOf(".") + 1).ToLower();                string 大图上传完整路径 = Server.MapPath("上传图片/大图/") + 新文件名 + @"." + 文件名后缀;
                    商品大图.SaveAs(大图上传完整路径);
                    SqlDataSource2.UpdateParameters["商品大图地址"].DefaultValue = "上传图片/大图/" + 新文件名 + @"." + 文件名后缀;
                }
                else
                {
                    SqlDataSource2.UpdateParameters["商品大图地址"].DefaultValue = "22";
                }            FileUpload 商品中图 = (FileUpload)DetailsView1.FindControl("商品中图上传");            if (商品中图.HasFile)
                {
                    string datetime = DateTime.Now.Date.ToString("yyyyMMdd");
                    string time = DateTime.Now.ToShortTimeString().Replace(":", "");
                    string 新文件名 = datetime + time + DateTime.Now.Millisecond.ToString();
                    string 文件名 = 商品中图.FileName;
                    string 文件大小 = 商品中图.PostedFile.ContentLength.ToString();
                    string 文件名后缀 = 文件名.Substring(文件名.LastIndexOf(".") + 1).ToLower();                string 中图上传完整路径 = Server.MapPath("上传图片/中图/") + 新文件名 + @"." + 文件名后缀;
                    商品中图.SaveAs(中图上传完整路径);
                    SqlDataSource2.UpdateParameters["商品中图地址"].DefaultValue = "上传图片/中图/" + 新文件名 + @"." + 文件名后缀;
                }
                else
                {
                    SqlDataSource2.UpdateParameters["商品中图地址"].DefaultValue = "33";
                }
                查询类.查询类 查询类1 = new 查询类.查询类();            SqlDataSource2.UpdateParameters["商品添加IP"].DefaultValue = 查询类1.获取客户端IP地址();
                SqlDataSource2.UpdateParameters["商品详细说明"].DefaultValue = ((CuteEditor.Editor)DetailsView1.FindControl("商品详细说明")).Text;            Label1.Text = SqlDataSource2.UpdateParameters["商品编号"].DefaultValue;            DetailsView1.Visible = false;
                GridView1.DataBind();
                //SqlDataSource1.DataBind();
                //GridView1.DataBind();
                GridView1.Visible = true;        }        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
            {
                GridView1.DataBind();                  }        protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
            {
                GridView1.DataBind();                    }
        }
    }
      

  3.   

    page指令中加入EnableViewState="false"
    http://topic.csdn.net/u/20090509/15/350e1b9a-431e-476e-ba3a-3799f1371f88.html
      

  4.   

    未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。 我用GridView1和DetailsView1实现商品列表以及具体商品的编辑 删除功能,在实现了一次编辑更新成功后,再次点击某个商品后出现了以上错误提示 
      

  5.   

    没有看你的代码,那么多代码看着头疼。如果可能,你最好先调试,定位问题的具体范围。通常这有两个原因:1. 你动态创建的控件,在Add到Controls之前,必须设计准确的ID属性。其它属性可以在Add之后设置,但是唯独ID绝对应该提前设置好,然后才Add。2. 你可能用javascript胡乱修改了控件的位置。
      

  6.   

    如果你没有动态创建的控件,也没有javascript移动后台控件生成的dom element的位置,那么就看看你是不是删除了服务器控件的ID(或者手写的代码缺少ID)?!
      

  7.   

    哪位大哥给我一个GridView1和DetailsView1实现商品列表以及具体商品的编辑 删除功能的例子,其中用到模板列,我自己对照学习一下。
      

  8.   

    我用GridView1和DetailsView1,实现商品列表以及具体商品的编辑、删除功能,在点击GridView1中的具体商品后,GridView1.Visible = false,而DetailsView1.Visible = true,在更新后GridView1.Visible= true;DetailsView1.Visible = false;但我发现不能在DetailsView1_ItemUpdated事件里写DetailsView1.Visible = false;
    否则 在实现了一次编辑更新后,再次点击GridView1某个具体商品就会出现 “未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。”其中 DetailsView1里面我用了模板列。是什么原因,应该怎么写?如果在page指令中加入EnableViewState="false" ,GridView1的隐藏也会不正常。