gpt4 book ai didi

c# - 遍历 NewValues 集合并对所有内容进行 HTML 编码

转载 作者:行者123 更新时间:2023-11-30 22:42:06 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com