首先,我看到有人提出了类似的问题,相信我,我已经阅读了答案和评论并试图理解。否则我不会发帖。
那么问题来了:
我有一个想要更新的网格视图。
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