<asp:formview ID="Formview1" runat="server" AllowPaging="True" 
            onpageindexchanging="Formview1_PageIndexChanging" 
            onprerender="Formview1_PreRender" onitemdeleting="Formview1_ItemDeleting" 
            DataKeyNames="GoodsID">
            <ItemTemplate>
                <b><%# Eval("GoodsID")%>:<%# Eval("GoodsName")%></b><small><%# Eval("GoodsIntroduce")%><br/><%# Eval("GoodsPrice")%><br/><br />
                <asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete" />
                </small>
            </ItemTemplate>
            <PagerSettings FirstPageText="首页" LastPageText="尾页" 
                Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
        </asp:formview>
        string ConnectionString = WebConfigurationManager.ConnectionStrings["db_04"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        public void bind()
        {
            string sqlstr = "select * from tb_GoodsInfo";
            SqlConnection sqlcon = new SqlConnection(ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet ds = new DataSet();
            sqlcon.Open();
            adapter.Fill(ds, "tb_GoodsInfo");
            Formview1.DataSource = ds;
            Formview1.DataBind();
            sqlcon.Close();
        }
        protected void Formview1_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {
            Formview1.PageIndex = e.NewPageIndex;
            bind();
        }
        protected void Formview1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
        {
            foreach (var item in e.Keys)
            {
                Response.Write(item.ToString()+"<br/>");
            }
            bind();
        }

解决方案 »

  1.   

    步骤如下:
    1、不按分页按钮,点击删除按钮后可获取key
    2、按分页按钮后,点击删除按钮第1次获取不到key,点击第2次才能获取key
    不知道自己是否少设置了什么,请有经验的帮我看看,谢谢!
      

  2.   

    用你的代码,我怎么获取不到key,keys count为0呢我没用过formview
      

  3.   

    为了方便帮忙回答前辈们看代码找出问题,我把查看更新都省略掉了,这2个部分获取key没问题的
      

  4.   


    没怎么用过这个控件。。你的删除代码没看见你可以在itemtemplate里放个隐藏的Labellabel中放入IDFormview1_ItemDeleting事件中Label lblID = (Label)this.FormView1.FindControl("Label的ID");然后再进行删除操作
      

  5.   

    我没发现你使用了sqldatasource数据源控件,没定义其delete 和 update的SQL语句,哪来的key呢,麻烦你将源代码 打包发给看看,Q:442962355
      

  6.   

    http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.formviewdeleteeventargs.aspx
      

  7.   

    要是找不到好的解决办法,只能用隐藏域等来FindControl了
      

  8.   

    key在前台页面里写DataKeyNames="GoodsID",数据源在自己写的bind()函数内
      

  9.   

    因为获取不到key,所以删除代码被我删了,专心解决key获取问题
      

  10.   

    我说的Key是FormViewUpdateEventArgs.Keys 和 FormViewDeleteEventArgs.Keys你将你1L提到的操作中,关于对这个Key的获取的代码,发来看看
      

  11.   


           <asp:formview ID="Formview1" runat="server" AllowPaging="True" 
                onpageindexchanging="Formview1_PageIndexChanging" 
                onitemdeleting="Formview1_ItemDeleting" 
                oniteminserting="Formview1_ItemInserting" 
                onitemupdating="Formview1_ItemUpdating" 
                onmodechanging="Formview1_ModeChanging" 
                onitemdeleted="Formview1_ItemDeleted" DataKeyNames="GoodsID">
                <HeaderTemplate>
                    信息:
                </HeaderTemplate>
                <ItemTemplate>
                    <b><%# Eval("GoodsID")%>:<%# Eval("GoodsName")%></b><small><%# Eval("GoodsIntroduce")%><br/><%# Eval("GoodsPrice")%><br/><br /><asp:Button ID="New" runat="server" Text="新建" CommandName="New" />
                    <asp:Button ID="Edit" runat="server" Text="编辑" CommandName="Edit" />
                    <asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete" />
                    </small>
                </ItemTemplate>
                <InsertItemTemplate>
                    名称:<asp:TextBox ID="GoodsName" runat="server" Text="" /><br/>
                    类型:<asp:TextBox ID="GoodsIntroduce" runat="server" Text="" /><br/>
                    价格:<asp:TextBox ID="GoodsPrice" runat="server" Text="" />   
                    <br/>    
                    <asp:Button ID="Insert" runat="server" Text="添加" CommandName="Insert" />
                    <asp:Button ID="Cancel" runat="server" Text="取消" CommandName="Cancel" /> 
                </InsertItemTemplate>
                <EditItemTemplate>
                    <%# Eval("GoodsID")%>:<br/>
                    <asp:TextBox ID="GoodsName" runat="server" Text='<%# Bind("GoodsName") %>' />
                    <br />
                    <asp:TextBox ID="GoodsIntroduce" runat="server" 
                        Text='<%# Bind("GoodsIntroduce") %>' />
                    <br />
                    <asp:TextBox ID="GoodsPrice" runat="server" Text='<%# Bind("GoodsPrice") %>' />
                    <br />
                    <br />
                    <asp:Button ID="Update" runat="server" Text="更新" CommandName="Update" />
                    <asp:Button ID="Cancel" runat="server" Text="取消" CommandName="Cancel" />
                </EditItemTemplate>
                <PagerSettings FirstPageText="首页" LastPageText="尾页" 
                    Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
            </asp:formview>       string ConnectionString = WebConfigurationManager.ConnectionStrings["db_04"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
            public void bind()
            {
                string sqlstr = "select * from tb_GoodsInfo";
                SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                sqlcon.Open();
                adapter.Fill(ds, "tb_GoodsInfo");
                Formview1.DataSource = ds;
                Formview1.DataBind();
                sqlcon.Close();
            }        protected void Formview1_PageIndexChanging(object sender, FormViewPageEventArgs e)
            {
                Formview1.PageIndex = e.NewPageIndex;
                bind();
            }        protected void Formview1_PreRender(object sender, EventArgs e)
            {
            }        protected void Formview1_ModeChanging(object sender, FormViewModeEventArgs e)
            {
                Formview1.ChangeMode(e.NewMode);
                bind();
            }        protected void Formview1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
            {
                Int64 id = (Int64)e.Keys["GoodsID"];
                string sqlstr = "update tb_GoodsInfo set"
                    + " GoodsName = '" + ((TextBox)(Formview1.Row.FindControl("GoodsName"))).Text.ToString().Trim() + "',"
                    + " GoodsIntroduce = ' " + ((TextBox)(Formview1.Row.FindControl("GoodsIntroduce"))).Text.ToString().Trim() + "',"
                    + " GoodsPrice = " + ((TextBox)(Formview1.Row.FindControl("GoodsPrice"))).Text.ToString().Trim()
                    + " where GoodsID = " + id;            SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();            Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();        }        protected void Formview1_ItemInserting(object sender, FormViewInsertEventArgs e)
            {
                string GoodsName = ((TextBox)Formview1.Row.FindControl("GoodsName")).Text.ToString();
                string GoodsIntroduce = ((TextBox)Formview1.Row.FindControl("GoodsIntroduce")).Text.ToString();
                string GoodsPrice = ((TextBox)Formview1.Row.FindControl("GoodsPrice")).Text.ToString();            string sqlstr = String.Format("insert into tb_GoodsInfo(GoodsName,GoodsIntroduce,GoodsPrice)values('{0}','{1}',{2})", GoodsName, GoodsIntroduce, GoodsPrice);            SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
                Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();
            }        protected void Formview1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
            {
                //String keyValue = e.Keys["EmployeeID"].ToString();            foreach (var item in e.Keys)
                {
                    Response.Write(item.ToString()+"<br/>");
                }
                //Int64 id = (Int64)e.Keys["GoodsID"];
                //string sqlstr = "delete from tb_GoodsInfo where GoodsID = " + id;
                //SqlConnection sqlcon = new SqlConnection(ConnectionString);
                //SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                //sqlcon.Open();
                //sqlcom.ExecuteNonQuery();
                //sqlcon.Close();            //Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();
            }
      

  12.   


            string ConnectionString = WebConfigurationManager.ConnectionStrings["db_04"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
            public void bind()
            {
                string sqlstr = "select * from tb_GoodsInfo";
                SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                sqlcon.Open();
                adapter.Fill(ds, "tb_GoodsInfo");
                Formview1.DataSource = ds;
                Formview1.DataBind();
                sqlcon.Close();
            }        protected void Formview1_PageIndexChanging(object sender, FormViewPageEventArgs e)
            {
                Formview1.PageIndex = e.NewPageIndex;
                bind();
            }        protected void Formview1_ModeChanging(object sender, FormViewModeEventArgs e)
            {
                Formview1.ChangeMode(e.NewMode);
                bind();
            }        protected void Formview1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
            {
                Int64 id = (Int64)e.Keys["GoodsID"];
                string sqlstr = "update tb_GoodsInfo set"
                    + " GoodsName = '" + ((TextBox)(Formview1.Row.FindControl("GoodsName"))).Text.ToString().Trim() + "',"
                    + " GoodsIntroduce = ' " + ((TextBox)(Formview1.Row.FindControl("GoodsIntroduce"))).Text.ToString().Trim() + "',"
                    + " GoodsPrice = " + ((TextBox)(Formview1.Row.FindControl("GoodsPrice"))).Text.ToString().Trim()
                    + " where GoodsID = " + id;            SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();            Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();        }        protected void Formview1_ItemInserting(object sender, FormViewInsertEventArgs e)
            {
                string GoodsName = ((TextBox)Formview1.Row.FindControl("GoodsName")).Text.ToString();
                string GoodsIntroduce = ((TextBox)Formview1.Row.FindControl("GoodsIntroduce")).Text.ToString();
                string GoodsPrice = ((TextBox)Formview1.Row.FindControl("GoodsPrice")).Text.ToString();            string sqlstr = String.Format("insert into tb_GoodsInfo(GoodsName,GoodsIntroduce,GoodsPrice)values('{0}','{1}',{2})", GoodsName, GoodsIntroduce, GoodsPrice);            SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
                Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();
            }        protected void Formview1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
            {
                //String keyValue = e.Keys["EmployeeID"].ToString();            foreach (var item in e.Keys)
                {
                    Response.Write(item.ToString()+"<br/>");
                }
                //Int64 id = (Int64)e.Keys["GoodsID"];
                //string sqlstr = "delete from tb_GoodsInfo where GoodsID = " + id;
                //SqlConnection sqlcon = new SqlConnection(ConnectionString);
                //SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                //sqlcon.Open();
                //sqlcom.ExecuteNonQuery();
                //sqlcon.Close();            //Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();
            }
           <asp:formview ID="Formview1" runat="server" AllowPaging="True" 
                onpageindexchanging="Formview1_PageIndexChanging" 
                onitemdeleting="Formview1_ItemDeleting" 
                oniteminserting="Formview1_ItemInserting" 
                onitemupdating="Formview1_ItemUpdating" 
                onmodechanging="Formview1_ModeChanging" 
                DataKeyNames="GoodsID">
                <HeaderTemplate>
                    信息:
                </HeaderTemplate>
                <ItemTemplate>
                    <b><%# Eval("GoodsID")%>:<%# Eval("GoodsName")%></b><small><%# Eval("GoodsIntroduce")%><br/><%# Eval("GoodsPrice")%><br/><br /><asp:Button ID="New" runat="server" Text="新建" CommandName="New" />
                    <asp:Button ID="Edit" runat="server" Text="编辑" CommandName="Edit" />
                    <asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete" />
                    </small>
                </ItemTemplate>
                <InsertItemTemplate>
                    名称:<asp:TextBox ID="GoodsName" runat="server" Text="" /><br/>
                    类型:<asp:TextBox ID="GoodsIntroduce" runat="server" Text="" /><br/>
                    价格:<asp:TextBox ID="GoodsPrice" runat="server" Text="" />   
                    <br/>    
                    <asp:Button ID="Insert" runat="server" Text="添加" CommandName="Insert" />
                    <asp:Button ID="Cancel" runat="server" Text="取消" CommandName="Cancel" /> 
                </InsertItemTemplate>
                <EditItemTemplate>
                    <%# Eval("GoodsID")%>:<br/>
                    <asp:TextBox ID="GoodsName" runat="server" Text='<%# Bind("GoodsName") %>' />
                    <br />
                    <asp:TextBox ID="GoodsIntroduce" runat="server" 
                        Text='<%# Bind("GoodsIntroduce") %>' />
                    <br />
                    <asp:TextBox ID="GoodsPrice" runat="server" Text='<%# Bind("GoodsPrice") %>' />
                    <br />
                    <br />
                    <asp:Button ID="Update" runat="server" Text="更新" CommandName="Update" />
                    <asp:Button ID="Cancel" runat="server" Text="取消" CommandName="Cancel" />
                </EditItemTemplate>
                <PagerSettings FirstPageText="首页" LastPageText="尾页" 
                    Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
            </asp:formview>
      

  13.   

    public void bind()
            {
                string sqlstr = "select * from tb_GoodsInfo";
                SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                sqlcon.Open();
                adapter.Fill(ds, "tb_GoodsInfo");
                Formview1.DataSource = ds;
                Formview1.DataBind();
                sqlcon.Close();
            }
    =》
    public void bind()
            {
                string sqlstr = "select * from tb_GoodsInfo";
                SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "tb_GoodsInfo");
                Formview1.DataSource = ds;
                Formview1.DataBind();            
            }
      

  14.   

    [email protected]  看一下
      

  15.   

    不具体看你这个了。我建议你看看使用ObjectDatasource或者SqlDatasource的人家的范例。少写代码。不论是删除,还是翻页,都不需要写一行代码,交给DataSource控件去处理。因为它们是经过严格测试、千锤百炼的。而你写的代码,今天对了明天错了,你的能力发挥非常不稳定。完全可以不写而开发代码时,去手写好多代码编程,这当然就会有很多错误。
      

  16.   

    首页,你获取Key的代码,是不能执行的,即Deleting和Updating事件里面的e.Keys.Count 为 0就是你13L的代码,Formview1_ItemUpdating事件里面的Int64 id = (Int64)e.Keys["GoodsID"];
    执行后将报 为将对象引用设置到对象的实例,就是说名为GoodsID的Key不存在,你似乎没搞清楚Deleting和Updating事件里面的e.Keys是取自哪里,参考SqlDataSource数据源控件的使用和MSDN的说明,如何你打算还是这样用自定义数据绑定和自定义操作,你可以用隐藏域标识你Update和Delete操作的记录项的ID比如你可以这样:
    <asp:FormView ID="Formview1" runat="server" AllowPaging="True" OnPageIndexChanging="Formview1_PageIndexChanging"
            OnItemDeleting="Formview1_ItemDeleting" OnItemInserting="Formview1_ItemInserting"
            OnItemUpdating="Formview1_ItemUpdating" OnModeChanging="Formview1_ModeChanging"
            DataKeyNames="GoodsID">
            <HeaderTemplate>
                信息:
            </HeaderTemplate>
            <ItemTemplate>
                <asp:HiddenField ID="hfldGoodsID" runat="server" Value='<%# Eval("GoodsID")%>' />
                编号:<%# Eval("GoodsID")%>
                <br />
                名称:<%# Eval("GoodsName")%>
                <br />
                介绍:<%# Eval("GoodsIntroduce")%>
                <br />
                价格:<%# Eval("GoodsPrice")%>
                <br />
                <br />
                <asp:Button ID="New" runat="server" Text="新建" CommandName="New" />
                <asp:Button ID="Edit" runat="server" Text="编辑" CommandName="Edit" />
                <asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete" />
            </ItemTemplate>
            <InsertItemTemplate>
                名称:<asp:TextBox ID="GoodsName" runat="server" Text="" /><br />
                介绍:<asp:TextBox ID="GoodsIntroduce" runat="server" Text="" /><br />
                价格:<asp:TextBox ID="GoodsPrice" runat="server" Text="" />
                <br />
                <asp:Button ID="Insert" runat="server" Text="添加" CommandName="Insert" />
                <asp:Button ID="Cancel" runat="server" Text="取消" CommandName="Cancel" />
            </InsertItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="GoodsID" runat="server" Text='<%# Bind("GoodsID") %>' Enabled="false" />
                <br />
                <asp:TextBox ID="GoodsName" runat="server" Text='<%# Bind("GoodsName") %>' />
                <br />
                <asp:TextBox ID="GoodsIntroduce" runat="server" Text='<%# Bind("GoodsIntroduce") %>' />
                <br />
                <asp:TextBox ID="GoodsPrice" runat="server" Text='<%# Bind("GoodsPrice") %>' />
                <br />
                <br />
                <asp:Button ID="Update" runat="server" Text="更新" CommandName="Update" />
                <asp:Button ID="Cancel" runat="server" Text="取消" CommandName="Cancel" />
            </EditItemTemplate>
            <PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NextPreviousFirstLast"
                NextPageText="下一页" PreviousPageText="上一页" />
        </asp:FormView>protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
            public void bind()
            {
                string sqlstr = "select * from tb_GoodsInfo";
                SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                sqlcon.Open();
                adapter.Fill(ds, "tb_GoodsInfo");
                Formview1.DataSource = ds;
                Formview1.DataBind();
                sqlcon.Close();
            }        protected void Formview1_PageIndexChanging(object sender, FormViewPageEventArgs e)
            {
                Formview1.PageIndex = e.NewPageIndex;
                bind();
            }        protected void Formview1_ModeChanging(object sender, FormViewModeEventArgs e)
            {
                Formview1.ChangeMode(e.NewMode);
                bind();
            }        protected void Formview1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
            {
                string id = ((TextBox)(Formview1.Row.FindControl("GoodsID"))).Text.Trim();
                string sqlstr = "update tb_GoodsInfo set"
                    + " GoodsName = '" + ((TextBox)(Formview1.Row.FindControl("GoodsName"))).Text.ToString().Trim() + "',"
                    + " GoodsIntroduce = ' " + ((TextBox)(Formview1.Row.FindControl("GoodsIntroduce"))).Text.ToString().Trim() + "',"
                    + " GoodsPrice = " + ((TextBox)(Formview1.Row.FindControl("GoodsPrice"))).Text.ToString().Trim()
                    + " where GoodsID = " + id;            SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();            Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();        }        protected void Formview1_ItemInserting(object sender, FormViewInsertEventArgs e)
            {
                string GoodsName = ((TextBox)Formview1.Row.FindControl("GoodsName")).Text.ToString();
                string GoodsIntroduce = ((TextBox)Formview1.Row.FindControl("GoodsIntroduce")).Text.ToString();
                string GoodsPrice = ((TextBox)Formview1.Row.FindControl("GoodsPrice")).Text.ToString();            string sqlstr = String.Format("insert into tb_GoodsInfo(GoodsName,GoodsIntroduce,GoodsPrice)values('{0}','{1}',{2})", GoodsName, GoodsIntroduce, GoodsPrice);            SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
                Formview1.ChangeMode(FormViewMode.ReadOnly);
                bind();
            }        protected void Formview1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
            {
                string id = ((HiddenField)(Formview1.Row.FindControl("hfldGoodsID"))).Value.Trim();            string sqlstr = "delete from tb_GoodsInfo where GoodsID = " + id;
                SqlConnection sqlcon = new SqlConnection(ConnectionString);
                SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
                bind();
            }
      

  17.   

    上述代码,我用了一个隐藏域 和 一个 Enabled="false"的textbox,你根据需求自己决定怎么标识主键上述代码已经测试,没问题
    提几点,.Text.ToString().Trim(),这种结构,Text属性返回的已经是string类型,你干嘛还要ToString()还有,数据库相关操作,你提取出来,放到一个类中(如SQLHelper.cs),这样代码会比较清爽,提高复用