3

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" %>
joshwbrick
  • 5,882
  • 9
  • 48
  • 72

3 Answers3

8

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" %>
Krisc
  • 1,357
  • 2
  • 14
  • 22
3

You can add an import page directive near the top of your .aspx page.

E.g.,

<%@ Import Namespace="System.Collections.Generic"%>
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
2

Add this to the top of the *.aspx page

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

killerbarney
  • 947
  • 10
  • 24
womp
  • 115,835
  • 26
  • 236
  • 269