Archive for the ‘ASP.NET’ Category

Inline Recursion in ASP.NET Template to Print a Tree’s Node List

Tuesday, September 14th, 2010

Here’s a neat & clean way to perform a recursion in ASP.NET, inside the .aspx/.ascx file. Suitable for ASP.NET MVC as well as WebForms.

No <asp:Repeater> + OnItemDataBound event, TreeView or concatenating strings recursively in the code behind involved.

Data Structure and Recursive Data Sample

public class NodeList : List<Node> {
}
public class Node {
	public string Name { get; set; }
	public NodeList Children { get; set; }
}
public class Repository {
	public static NodeList GetNodes() {
		return new NodeList {
			new Node { Name = "1",
				Children = new NodeList {
					new Node { Name = "1.1" },
					new Node { Name = "1.2",
						Children = new NodeList {
							new Node { Name = "1.2.1" }
						}
					},
					new Node { Name = "1.3" },
					new Node { Name = "1.4" },
				}
			},
			new Node { Name = "2",
				Children = new NodeList {
					new Node { Name = "2.1" }
				}
			},
			new Node { Name = "3" }
		};
	}
}

The Recursive Function

<%
Action<NodeList, int> printNodesRecursively = null;
printNodesRecursively = (NodeList nodeList, int depth) => {
	if (nodeList==null || nodeList.Count==0) return;
%>
	<ul class="node-list depth-<%=depth%>">
<%
	foreach (Node node in nodeList) {
	%>
	<li>
		<%=node.Name%>
		<%printNodesRecursively(node.Children, depth + 1);%>
	</li>
	<%
	}
%>
    </ul>
<%
};
NodeList nodes = Repository.GetNodes(); // Or ViewData use for ASP.NET MVC
printNodesRecursively(nodes, 0);
%>

Notice: The printNodesRecursively function is declared with the value of null at first. Otherwise, the compiler says that the inner printNodesRecursively call is made on an unassigned variable.

This will basically print a <ul> with an <li> for each node, and inside each <li>, another <ul> with its children, recursively.

Notice the extra “depth” which indicates the current depth level in the tree inside the printNodesRecursively function. In this example it adds a special class to each level.

The Output

<ul class="node-list depth-0">
	<li>1
		<ul class="node-list depth-1">
			<li>1.1</li>
			<li>1.2
				<ul class="node-list depth-2">
					<li>1.2.1</li>
				</ul>
			</li>
			<li>1.3</li>
			<li>1.4</li>
		</ul>
	</li>
	<li>2
		<ul class="node-list depth-1">
			<li>2.1</li>
		</ul>
	</li>
	<li>3</li>
</ul>

(Code re-indented manually :) )

Clear All ASP.NET Cache/Static Objects Programmatically

Tuesday, August 17th, 2010

I used to re-save Web.config or Recycle a website in IIS when I wanted to delete all ASP.NET website’s cache / reset all static objects, or basically restart the application.

This piece of code does it, programmatically:

HttpRuntime.Close();

So I created a little .ashx/MVC action to do so and access it through the browser.

Have in mind that this will reset the application, including Sessions and static variables, so never use it on production.

IIS7 File Upload Limit

Friday, August 13th, 2010

I encountered this while uploading a large file to an ASP.NET handler, seems like there’s a default limit of ~30MB for a single request in IIS7 on Windows 7.

After some researching, I found that there’s a setting inside <system.webServer> other than the regular limit in <httpRuntime maxRequestLength=".."/> (god and MS know why the duplication).

To fix that do one of the following:

Option 1: Add to your Web.config the following code, under the appropriate nodes:

<system.webServer>
	<security>
		<requestFiltering>
			<requestLimits maxAllowedContentLength="2147483648" />
		</requestFiltering>
	</security>
</system.webServer>

Option 2: Go to IIS 7 control panel, under your website look for Request Filtering and then Edit features (on the right), then set Maximum allowed content length to your desired limit. This will add to your Web.config the lines from option 1.

Actually I had this while playing with FancyUpload (great thing!) and got Error #2038 on every request.