开发者

Iterate through the NewValues collection and HTML encode all

I have a Gridview and I try to Iterate through the NewValues collection and HTML encode all.

I am following MSDN CODE.... using their code (posted here) I receive an error:

Collection was modified; enumeration operation may not execute.

I would like ask you a Full simple example ho开发者_Go百科w to implement it, so beginners like me can start use this function. PS: I posted a similar questions here and people replied but I still do not understand it and i Would need a simple example.

    <%@ 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>


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());
}


You cannot change collection while iterating through it using foreach loop.

 foreach (DictionaryEntry entry in e.NewValues)
{
  e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
}

You are iterating through e.NewValues and changing it in next line. You have to create copy of e.NewValues, change it and then assign e.NewValues with the changed copy.

You could use a for loop:

for (int i=0; i< e.NewValues.Count; i++)
{
  DictionaryEntry entry = (DictionaryEntry)e.NewValues[i];
  e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
}

* EDIT: It doesn't work, I tried to cast e.NewValues to DictionaryEntry with no success. On the other hand the code posted by GIbboK works fine.


I couldn't get it to work using Tomasz's approach (the iteration worked fine and looked like the values were updated, but the actual change did not take affect for the new values).

I performed the change on the DataSource control instead:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜