Location: BlogsComments    
Posted by: Dale Monday, January 17, 2011
Various things I've run into building sites in ASP.NET. Enhanced Validation, Tableless FormView, sneaky binding to page variables, ASP.NET Caching GET/POST.

ASP.NET Caching GET/POST. ASP.NET provides some very good tools for caching, but its an area that is often misunderstood and often not taken advantage of. For a recent site I wanted most pages cached, at least on the GET request. However certain controls, namely a search box, appeared on every page and needed to be processed properly by the server. Turning caching on at all locations didn't do the job because while the client will always request a POST request, the server at best would cache the page based on the form fields. That was until I discovered Response.Cache.AddValidationCallback ( which allows you to add some logic of your choice to the determination as to whether a page is in the cache or not. A simple check of Request.HttpMethod == "POST" allowed me to ignore all cached pages on POST and make full use of the cache on GET.

Enhanced Validators. I had a recent need to change the input & label style on validation. This isn't possible out of the box with validators, but lucky for me someone had already done the hard work.Check out Enhanced Validation by Ronald Partridge. It worked pretty much out of the box, but as I had a couple of extra requirements I ended up having to modify the code (see my comment at the bottom). I also added a checkbox validator and a textbox length validator. Happy to supply code as is - just contact me.

Using FormView always renders with a table. There are some ways to override the rendering method of ASP.NET controls but they do introduce additional complexity. I found the easiest solution to remove the tables was to use a repeater control and only display 1 record. It doesn't do everything that the FormView does but it does quite a bit. Update: The latest version of .NET provides far more control over rendering. Or there is the CSSFriendly adaptor.

Minimise binding with ASP.NET controls. In the past I've often found myself databinding using a datasource control to say a repeater control and then having to do a manual query on the database to obtain say the page title or similar. Then I realised that you can bind the field as <%#BindMore((String)Eval("Field1"), (String)Eval("Field2")%> inside the databound control and then in BindMore save the values for use later in say PreRender, saving a trip to the database.

Using Session State in HttpHandlers e.g. WebHandlers is ASHX files. Remember to implement IReadOnlySessionState.

<% @ webhandler language="C#" class="DownloadHandler" %>using System;
using System.Web;
using System.Web.SessionState;

public class MyHandler : IHttpHandler, IReadOnlySessionState
public bool IsReusable { get { return true; } }

public void ProcessRequest(HttpContext Context)

Thanks to Scott Hanselman

Data Formatting ASP.NET 2.0 Gridview: I recently had a problem formatting data in a Gridview and found (thanks to this article: that because formatting occurs after the HTML encode function (which is on by default) the formatting has no effect. Setting HtmlEncoding to 'false' solves the problem.

Error "Cannot use a leading .. to exit above the top directory.": This may happen when re-writing URL's in which case for the solution see this thread: DNN Forum Post and this blog: ToDotNet Blog Post

Error " Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. ": If you are sure that the error is not caused by the points mentioned then check this DotNetSlackers Blog Post and this ASP.NET Forums Post

Exception has been thrown by the target of an invocation. at System.RuntimeMethodHandle._InvokeMethodFast(...) Error received when adding a new user to an membership application. This is due to using "encryption' and not having an available machine key. You can create a machine key here: Machine Key Creator

Permalink |  Trackback

Your name:
Add Comment   Cancel