Search code examples
c#asp.netimportenumerate

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:

<% 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?)

How do I import System.Collections.Generic into the page itself? Here is my page directive:

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

Solution

  • You can do this a number of ways.

    In your web.config you can add System.Collections.Generic to the namespaces element.

    You can also reference it directly as 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" %>