简体   繁体   English

在ASP.NET网页中枚举字典

[英]Enumerate Dictionary Within ASP.NET Web Page

I have a ASP.NET (C#) web page in which I want to enumerate a dictionary in a code render block: 我有一个ASP.NET(C#)网页,我想在代码渲染块中枚举一个字典:

<% foreach (Dictionary<string, string> record in parsedData) { %>
     <div>...Some HTML Code...</div>
<% } %>

But I get an error like: 但我得到一个错误:

Compiler Error Message: CS0246: The type or namespace name 'Dictionary' could not be found (are you missing a using directive or an assembly reference?) 编译器错误消息:CS0246:找不到类型或命名空间名称“Dictionary”(您是否缺少using指令或程序集引用?)

How do I import System.Collections.Generic into the page itself? 如何将System.Collections.Generic导入页面本身? Here is my page directive: 这是我的页面指令:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyCSharpClass.aspx.cs" Inherits="_MyCSharpClass" %>

You can do this a number of ways. 你可以通过多种方式实现这一目标。

In your web.config you can add System.Collections.Generic to the namespaces element. 在web.config中,您可以将System.Collections.Generic添加到namespaces元素。

You can also reference it directly as System.Collections.Generic.Dictionary<string, string> 您也可以直接将其引用为System.Collections.Generic.Dictionary<string, string>

You should also be able to do it on the page directly (though I must admit I haven't tested this way): 你也应该能够直接在页面上做这件事(虽然我必须承认我没有用这种方式测试过):

<%@ Import Namespace="System.Collections.Generic" %>

You can add an import page directive near the top of your .aspx page. 您可以在.aspx页面顶部附近添加import页面指​​令。

Eg, 例如,

<%@ Import Namespace="System.Collections.Generic"%>

将其添加到* .aspx页面的顶部

<%@ Import Namespace="System.Collections.Generic" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM