Posts

Showing posts from February, 2010

Convert serialzed JSON date string to a Javascript Date() object

When an DateTime object in C# is converted into a JSON serialized object for a web page, the format of the string looks like this in the JSON object: /Date( )/ See this for more details on why this format was chosen. This is all well and good, but how do you use that? A similar question and answer can be found here , but the solution posted there misses the possibility of a negative int. To expand just a bit on the solution posted there, we'll account for negative numbers: var someDate = new Date(+jsonDate.replace(/\/Date\((-?\d+)\)\//gi, "$1")); The three key things to note here are: 1. This method does not use eval(). eval can be a very dangerous thing to use since it will execute any javascript, so generally we try to avoid using it altogether. This is why we use JSON.parse, for example, instead of eval. 2. The "+" operator is a type converter in javascript that converts the string to an int. 3. The "-?" because the date value c

Cascading DropDowns w/ JQuery ViewState Problem - use the UpdatePanel w/ triggers instead

I spent a lot of time trying to resolve a DOM issue I was having with a cascading drop downs setup on my page.  After mucking around with it for quite a while, I was able to figure out that it wasn't a direct effect of my code, but seemed to be a result of some DOM corruption that was happening from a plugin I was using or something else (that would be very time consuming to uncover). I was making a web service (POST) call to update the 2nd drop down list... if ($(someDDObject).val().length > 0) { someGlobalVariable = $(someDDObject).val(); ExecuteCallback('DoneChanged|' + someGlobalVariable); } The ExecuteCallBack is tied to a server-side async, non-static function that would parse the input and return the updates. A nice clean way to get server-side data without a postback, right? Right...mostly. The problem came in trying to update the client side data. In the callback function on the client, I would process the data like so: function CallBackResul