Friday, April 11, 2008

Javascript onUnload and postback

So I just ran into an interesting scenario. I have a custom control where I need an event to fire before postback but I don't want to have to add an OnClientClick event in every page that uses the control. In fiddling with the javascript onunload event I found that oddly enough the unload event fires after postback to my aspx page. The solution? Use onbeforeunload instead:

window.onbeforeunload = unload;

function unload() {

Wednesday, April 2, 2008

Asp Uploader Control Weirdness

It's been a while since my last post and I apologize for that, I need to post more often because I tend to run across interesting things to post about. I'm just lazy.

I ran into an issue the other day when working on a SharePoint site that had me confused for a while. For some reason when you access the Uploader.PostedFile.InputStream property twice (even when setting the Position in the file to zero manually upon setting up a new reference to it and wrapping things in a using statement) the second time you read it you’ll get an empty file. Which is fucked up.

Anyhow, I fixed that by accessing the Uploader.PostedFile.InputStream property in one of the instances where I wanted to access the file but didn't actually need an input stream. I haven’t really been able to make sense out of why it works this way but not the other but whatever. Life goes on.

Friday, January 11, 2008

GDI and Ampersand Rendering

Yesterday I spent a while tracking down a bug in a library that I wrote a while back. One of the things that the library is used for is drawing text in fonts that aren't web safe to be used as headings and whatnot. So if I want the phrase "Out Of Office" drawn in whatever font I just created (this isn't actually a real use case) I can just upload the ttf file to my fonts directory on the server and use the control to generate an image dynamically that will fit the space. The tool even provides caching so after a string with a given set of params has been requested N times it caches it and will load the image from the cache upon future requests. But enough about that.

The bug I tracked down yesterday was related to the ampersand character and the fact that for some reason when the string I wanted to draw contained the ampersand character then no text was being drawn. I wouldn't get any exceptions or anything I just wouldn't get any text. The result image was just the background color I'd requested the size that the text would require if it was in the image. Without the ampersand char it worked as expected.

In GDI when you go to draw a string via Graphics.DrawString you can optionally display hot keys (meaning underlining a specific key to specify that it's a shortcut visually). When I'm drawing a string I'm passing in a StringFormat object which contains some formatting information (horizontal alignment and such). One of the properties of StringFormat is HotkeyPrefix which is used to set the Hotkey functionality for the string (Hide, Show, or None). Now you might think that simply setting it to None (which is the default btw) would result in no Hotkey formatting being considered when rendering the string. But you'd be wrong.

After a great deal of trial and error I got the ampersand to render when I replaced each ampersand with 2 ampersands (an escape of sorts) and then set HotkeyPrefix to Hide. Why this worked I'm not sure and it's kind of counterintuitive but whatever. That's how I fixed it.