Wednesday, March 18, 2009

TextChanged event of TextBox not raised by the Internet Explorer AutoComplete feature

As noted in this msdn article, The TextChanged event of a TextBox control may not fire if the AutoComplete feature is enabled in Internet Explorer. And it appears to be “by design” – I hate that.

Fortunately, it is pretty easy to add some JavaScript to make the ‘right thing’ happen.

All you need to do is raise the onchange event at the appropriate time. For me that is when the TextBox loses focus. As I am doing this from code behind (as usual) I am registering a client script, then when I add the TextBox I set its onblur attribute accordingly:

Page.ClientScript.RegisterClientScriptBlock(typeof(FormPage), "RaiseOnChange", "function RaiseOnChange(ctrl){ctrl.fireEvent('onchange');}", true);
tb.Attributes[HtmlAttributes.OnBlur] = string.Format("RaiseOnChange({0})", tb.UniqueID);

The server side TextChanged event only gets raised if the TextBox.Value has really changed. I needed to do this because I am using the TextChanged event of some TextBox controls to trigger an asyncpostback to update an UpdatePanel. The code that sets the content of the UpdatePanel is in the server side TextChanged handler.

Friday, March 13, 2009

Quoted strings

I posted something about this once before, but having just given myself a headache trying to fix something similar once again I feel like writing some more.

Using the quote delimiter character inside of a quoted string at always a minor problem. There are two ways you can fix it – you can escape the quote using whatever method is valid at the time* or you can change the type of quote delimiter, i.e. you can switch from " to ' or vice-versa. There is a deeper problem though that is only really seen when you are not just embedding a delimiter in a string, but (as seems to be often the case with web development) when you are passing such a string to someone or something else. Say for example you are writing ASP.Net code behind to output a __doPostback where one of the arguments is a string with an embedded delimiter. The string in .Net code will be delimited (say with ") then the argument to __doPostback will also be delimited (say with ') but then the data in that string might contain ' (e.g. O'Neil) which will have to be escaped.

My exact situation this time was slightly more complicated still – I have three levels of indirection! I am in c# code behind, setting the .Text property on a Telerik DataGridItem to be an html anchor tag which will do a postback. So the .Net string is delimited (with " naturally), the onclick of the a tag is delimited (with " also by convention, so escaped for .Net as \"), the argument to the __doPostback function is delimited (with ' because there is no way of escaping nested " characters for html) and finally the data in that argument might have ' characters so it needs to be escaped (by \')! And certain parts need to be UrlEncoded also, but before you put in the \' escaping or else you will UrlEncode the \ character. *Hum*

item[colName].Text = string.Format("<a href=\"#\" onclick=\"__doPostBack('', '{1}{0}{2}')\">{3}</a>", QueryString.PostBackArgSep, postbackType, item[colName].Text.Replace("'", "\\'"), item[colName].Text);

* Mmm, yes. Whatever method is valid at the time. This is not always straight forward with some situations calling for \ to give \' or \", some for a doubling up of the offending character giving '' or "", or even for escaping to be impossible in the current context, leaving you with only the ability to switch delimiters. This last problem with why I try to stick with " as the delimiter most of the time and escaping where required rather than the simpler change to ' so as to save the other character for situations where it is the only method (e.g. html). I think that it is probably possible to get yourself into a situation where there is no possible way to pass the string ‘down the line’ as it were without breaking something. If that happens either you need to encode and decode the whole string somewhere, or admit that you are doing something the hard way and find the right way to do it… I almost think that is what is going on here with my current problem – I think it could be solved by keeping the information that I am passing through to __doPostback somewhere else and just passing a key to it to the code I am calling, but I don’t have the time to do that right now, and this form will be soon superseded by an ASP.Net MVC version.

Wednesday, March 04, 2009

Virtual Root (~) gives 404 error

We had a server reboot after a power failure yesterday, and this morning an ASP.NET application that has been working for months started giving this error:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /~/direct.aspx


Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082

Seems to me that it is not translating the ~ into the Application Root path, but I am at a loss as to why. I have posted a question about the ~ virtual root giving a 404 on the ASP.NET newsgroup, but as is often the case with these WTF kind of posts I am not really expecting a response…

It was easily ‘fixed’ by just making the form action use the absolute path to the aspx file, but I want to know why it is broken.

Tuesday, March 03, 2009

Telerik RadUploadProgressHandler and ASP.NET MVC

Just ran into a small problem with the Telerik radUpload control and ASP.NET MVC.

The upload progress needs to have a httpHandler and httpModule configured in Web.config for it to work correctly – I think it is that which handles the GUID that it tacks onto the end of the url to maintain state (among other things). The path is normally Telerik.RadUploadProgressHandler.ashx but with MVC this will be routed to a controller and the handler will not be found.

I have added an exclusion to my Global.asax.cs file and it seems to work.

routes.IgnoreRoute("Telerik.RadUploadProgressHandler.ashx/{*pathInfo}");

Remains to be seen if this is the right way to do it or if there is anything else wrong, but for now I am just trying to get two existing web applications merged together as one MVC application. A lot of the stuff that those apps are doing is not “correct” from a MVC point of view, but this is version 0.9 :)


[Edit] Meh. Don’t need to do that. There are instructions on the Telerik site for how to configure the httpHandler for RadUpload under ASP.NET MVC

Shake to do something

What? That has to be the most stupid interface trick that I have ever heard of. Why the hell would I want to shake my iPhone to delete the current entry or shake it to start the recording? Jeebus. Perhaps if it was really sensitive and the shaking was totally in context with what you are using the device for, but shake to delete a database entry? That is just a stupid gimmick!

Monday, March 02, 2009

More iPhone icons

I am not quite happy with the template that I made for these icons… The corners are not quite right. But at least the first page of my applications all are about the same brightness now, and they all have the same border look. One for Twittelator* and one for gNotes:

Twittelator gNotes

Next!


* The channels of the png are in the wrong order, or a different order, or something on the iPhone so you have to use fixpng or something to fix it before you can open it for editing… but that seems to modify the colours (fair enough I guess, you are changing the channels…). I thought it was just a straight negative, but I don’t think it is. Rather than fiddling too much though, I quite like the colour of the Twittelator one as it is – better than the aqua and purple one that is the default anyway.

iPhone themes…

Wow, there are a bunch of themes out there for WinterBoard etc.

I am using iGlassSol which is very nice, but there are a couple of things I didn’t like. The clock could have a better time on it, and there was no icon for WideEmail. So I made some:

Clock WideEmail

The other thing that I made was a new dock with 5 icons on it… but it seems like there is a way to make it generate the glassy looking dock on the fly… but I can’t find it, only forum posts talking about it. Anyway, the dock (with WideEmail as the extra icon):

SBDockBG

Fun.