I have GridView and I need HTML ENCODE all values being update using Event handler _RowUpdating.
At the moment I use this script BUT I receive an error:
Collection was modified; enumeration operation may not execute.
Any idea how to make it works? Thanks for your help!
foreach (DictionaryEntry entry in e.Keys)
{
e.Keys[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
}
Three options:
1) Take a copy of all the entries to start with, so you’re not actually iterating over those and modifying them as you go.
2) Just iterate over a copy of the keys – again, to avoid modifying what you’re iterating over.
3) Build a new dictionary instead of modifying the existing one.
Given the skeleton code here:
… and taking “upper-casing” as a simple thing to test (just use
Server.HtmlEncodein your real code), here are the three options (I’m assuming you have LINQ available to you):1) Iterating over a copy of the entries:
2) Iterating over a copy of the keys:
3) Creating a new dictionary (and then assigning the reference back to the
dictvariable):