【问题标题】:How to edit an entry in the GridView using CommandField如何使用 CommandField 编辑 GridView 中的条目
【发布时间】:2015-02-18 01:51:58
【问题描述】:

我的 gridview 中有以下命令字段:

<asp:CommandField ShowEditButton="True" HeaderText="EDIT" CausesValidation="false"
    HeaderStyle-BackColor="#CCCCCC" HeaderStyle-CssClass="infoBoldBlueSmall">
    <HeaderStyle BackColor="#CCCCCC"></HeaderStyle>
</asp:CommandField>

条目如下所示:

当我点击编辑按钮时,我看到一个更新取消:

对于某些条目,当我单击编辑按钮时,出现以下错误:

Server Error in '/' Application.
--------------------------------------------------------------------------------

'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: 'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[ArgumentOutOfRangeException: 'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +2732917
   System.Web.UI.WebControls.ListControl.PerformSelect() +49
   System.Web.UI.Control.DataBindChildren() +11143343
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
   System.Web.UI.Control.DataBindChildren() +11143343
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +267
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3340
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +72
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +18
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +147
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +46
   System.Web.UI.Control.PreRenderRecursiveInternal() +108
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5483 

如果需要更多信息,请告诉我。

gridview 已填充,所以我知道数据在那里,我还检查了表格,它也在那里。

我该如何解决这个问题?

更新:

代码贴在这里:

http://pastebin.com/ezr9uNMc

显示 GridView 的图像:

当为每一行单击Edit 时会触发什么?如果我为 52 单击 Edit,它可以工作,但对于 57、58 和 59,我会收到上面的堆栈跟踪错误。

请帮我解决问题。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    拥有代码会有所帮助,但我之前可能遇到过这种情况。您可能在绑定行中的数据之前设置了选定项,因此您可能需要在 RowDataBound 事件中设置最初选定的项。

    如果不这样做,请确保您正在处理列表中没有任何项目的情况,因此您不想设置所选项目。

    【讨论】:

    • 我将在我的问题中发布代码,以便您更好地了解我在说什么。当它说it does not exist in the list of items时,它是什么意思?我查看了它用于显示行的查询,它存在于 SQL 表中。
    • 问题已更新。请让我知道...感谢您的帮助。
    【解决方案2】:

    问题一定是为每一行填充的下拉列表。
    当您单击编辑按钮时,下拉列表的选定值不存在。
    确保下拉项目中存在绑定值。

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2010-11-30
      • 2013-08-17
      • 2015-11-04
      • 2010-12-11
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多