gpt4 book ai didi

c# - Gridview Item has already been added 错误

转载 作者:行者123 更新时间:2023-11-30 20:01:52 25 4
gpt4 key购买 nike

首先,我看到有人提出了类似的问题,相信我,我已经阅读了答案和评论并试图理解。否则我不会发帖。

那么,接下来的问题是:

我有一个要更新的 GridView 。

ASP 代码如下所示:

<asp:GridView ID="myGv" runat="server" 
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" DataKeyNames="id" DataSourceID="myObjectDataSource"
OnRowUpdating="myGv_RowUpdating" >
<Columns>
<asp:BoundField DataField="id" HeaderText="id" Visible="false" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="type" HeaderText="type" SortExpression="type" ReadOnly="True" />
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" ReadOnly="True" />
<asp:BoundField DataField="volume" HeaderText="volume" SortExpression="volume" />
</Columns>
</asp:GridView>

这是对象数据源:

<asp:ObjectDataSource ID="myObjectDataSource" runat="server" 
DeleteMethod="myDeleteMethod" SelectMethod="mySelectMethod"
TypeName="url.sqlDataLayer" UpdateMethod="myUpdateMethod">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:Parameter Name="fromDate" Type="DateTime" />
<asp:Parameter Name="toDate" Type="DateTime" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="volume" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>

这是背后的代码 (C#):

protected void myGv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)myGv.Rows[e.RowIndex];

string value = ((TextBox)(row.Cells[3].Controls[0])).Text;

e.NewValues.Add("volume", value);
}

我在同一命名空间中有另一个页面,它具有完全相同的布局(具有相同的列名),这可能是导致错误的原因。

这是错误代码:

Item has already been added. Key in dictionary: 'volume'  Key being added: 'volume'
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.ArgumentException: Item has already been added. Key in dictionary: 'volume' Key being added: 'volume'

和堆栈跟踪:

[ArgumentException: Item has already been added. Key in dictionary: 'volume'  Key being added: 'volume']
System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) +14349425
System.Collections.Specialized.OrderedDictionary.Add(Object key, Object value) +144
namespace.class.myGv_RowUpdating(Object sender, GridViewUpdateEventArgs e) in theurl.aspx.cs:55
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1157
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +762
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +164
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

最佳答案

看起来很干脆,这是导致问题的行:

e.NewValues.Add("volume", value);

您告诉 GridView 将用户刚刚输入的值添加到一个名为 volume 的新值中,这是一个副本。

如果你想更新值,那么这样做:

e.NewValues["volume"] = whateverValueYouWantHere;

注意:通常,您会对该值进行某种类型的用户输入清理,例如对值进行 HTML 编码以降低跨站点脚本 (XSS) 攻击的风险。

关于c# - Gridview Item has already been added 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com