作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Gridview,我尝试遍历 NewValues 集合并对所有内容进行 HTML 编码。
我正在关注 MSDN CODE ....使用他们的代码(张贴在这里)我收到一个错误:
Collection was modified; enumeration operation may not execute.
我想问你一个完整的简单例子如何实现它,所以像我这样的初学者可以开始使用这个功能。PS:我发过类似的问题here人们回复了,但我仍然不明白,我需要一个简单的例子。
<%@ 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());
}
关于c# - 遍历 NewValues 集合并对所有内容进行 HTML 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4592051/
我是一名优秀的程序员,十分优秀!