Wednesday, March 22, 2006

File Upload Frustrations

In .NET 1.1 it is relatively simple to provide file upload functionality using an HtmlInputFile control e.g.

<input id="inputFile" type="file" runat="server">

Where I ran into problems was when the file exceeded the maximum size. This is controlled by the maxRequestLength attribuite of the httpRuntime element in the config file hierarchy. The default value is 4096 KB. After some expoerimentation I determined that the error could only be trapped by the Application_Error event handler in global.asax.cs. I wanted to redirect back to the calling page and display a message but I could not get this to work. Some research showed that I was not alone:

I ended up using Response.Write() to output an error message on the empty page that resulted from Server.ClearError() in the event handler (you need this to stop the error bubbling up to be handled as defined by the customErrors element in web.config). I added an <a> tag that contained a JavaScript link to take the user back to where they came from but was not particularly happy with this solution.

Tuesday, March 21, 2006

There has to be an easier way...

I am working on a C# application in .NET 1.1. I have a Repeater web control to which I bind an IList of objects. Upon postback I need to know the ID of the object that I bound so that can associate the data with the correct object. I went down a lot of dead ends first but eventually I stumbled upon this solution.

I created an event handler for the ItemDataBound event of the Repeater where I added the UniqueID of each control to a Hashtable. After all of the items were bound (PreRender event of the repeater) I stored the Hashtable in Viewstate. After postback I retrieve the Hashtable from Viewstate and look up the UniqueID of each RepeaterItem.

This is a relatively simple approach to reproduce but it seems like a lot of work and that the framework should be able to help me more. Any ideas?