C# 迭代NewValues集合并对所有

C# 迭代NewValues集合并对所有,c#,asp.net,C#,Asp.net,我有一个Gridview,我尝试遍历NewValues集合,并对所有内容进行HTML编码 我在跟踪。。。。使用他们的代码(张贴在此处),我收到一个错误: Collection was modified; enumeration operation may not execute. 我想问你一个完整的简单示例如何实现它,这样像我这样的初学者就可以开始使用这个函数了。 PS:我发布了一个类似的问题,人们回答了,但我仍然不理解,我需要一个简单的例子 我真的很感谢你的帮助 <%@ Pag

我有一个Gridview,我尝试遍历NewValues集合,并对所有内容进行HTML编码

我在跟踪。。。。使用他们的代码(张贴在此处),我收到一个错误:

Collection was modified; enumeration operation may not execute.
我想问你一个完整的简单示例如何实现它,这样像我这样的初学者就可以开始使用这个函数了。 PS:我发布了一个类似的问题,人们回答了,但我仍然不理解,我需要一个简单的例子

我真的很感谢你的帮助

    <%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {

    // Iterate through the NewValues collection and HTML encode all 
    // user-provided values before updating the data source.
    foreach (DictionaryEntry entry in e.NewValues)
    {

      e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowUpdating Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView RowUpdating Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdating="CustomersGridView_RowUpdating"  
        runat="server">
      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>

无效CustomerGridView\u行更新(对象发送方,GridViewUpdateEventArgs e)
{
//迭代NewValues集合并对所有
//用户在更新数据源之前提供的值。
foreach(e.NewValues中的DictionaryEntry条目)
{
e、 NewValues[entry.Key]=Server.HtmlEncode(entry.Value.ToString());
}
}
GridView行更新示例
GridView行更新示例

在使用foreach循环遍历集合时,不能更改集合

 foreach (DictionaryEntry entry in e.NewValues)
{
  e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
}
您正在迭代e.NewValues,并在下一行中对其进行更改。 您必须创建e.NewValues的副本,对其进行更改,然后将更改后的副本分配给e.NewValues

您可以使用for循环:

for (int i=0; i< e.NewValues.Count; i++)
{
  DictionaryEntry entry = (DictionaryEntry)e.NewValues[i];
  e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
}
for(int i=0;i
*编辑: 它不起作用,我试图将e.NewValues转换到DictionaryEntry,但没有成功。另一方面,GIbboK发布的代码运行良好。

尝试以下方法:

object[] keys = (object[])Array.CreateInstance(typeof(object), e.NewValues.Count);
e.NewValues.Keys.CopyTo(keys, 0);

foreach (object key in keys)
{
    e.NewValues[key] = Server.HtmlEncode(e.NewValues[key].ToString());
}

--Pavel

我无法使用Tomasz的方法使其工作(迭代运行良好,看起来值已更新,但实际更改并未对新值产生影响)

我改为对数据源控件执行了更改:

protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e) {
    foreach (DbParameter parameter in e.Command.Parameters) {
        parameter.Value = Server.HtmlEncode(parameter.Value);
    }
}

感谢Tomasz,我理解您的解释,但您的脚本(inti=0;I