Posts

Windows Live Toolbar broke my Shift+Tab

Finally!!!!!! For months my shift+tab in forms didn't work. IE or Firefox. Drove me crazy!!!!! Google a little this morning and found an entry that said uninstall Windows Live Toolbar. Thought "what the heck". It worked!!! Not only did fix IE but it also fixed FF. Don't understand. Don't care. Problem solved. Technorati Tags: Windows Live Toolbar Shift+Tab

Adding commas to numbers with JavaScript

I recently went to add commas to a number on entry. Found a nice script here . Only thing I changed was to make it set the value in the function. I also liked his solution for adding separators other than commas. The code: function addCommas(obj) { nStr = obj.value; nStr += '' ; x = nStr.split( '.' ); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : '' ; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2' ); } obj.value = x1 + x2; }

Parse CSV or other separated strings

I had to work with Comma Separated Values (CSV) files the last few days and updated some code to make it easier to use with LINQ. Quick review, a CSV file is a file that has all of the values separated by a comma. Values that contain a comma are typically qualified with double quotes. For example: Abraham, Lincoln, 03/4/1861, 04/15/1865, "Republican, National Union" Basically, I created a class called SeparatedString that performs all the functionality of parsing the data so that I can retrieve it later. When the Text property is set it figures out the location of all relevant commas and loads them into a List. The GetValue method then uses the index to grab the occurrence of the comma that precedes the value to be retrieved. Public Class SeparatedString ''' <summary> ''' Contains the position of all separator characters in the string ''' </summary> ''' <remarks></remarks> ...

Simple Age Calculation

I was helping someone with some SQL stuff and needed to give them a function to calculate someone's age.  Rooted around a bunch of places and finally just ended up re-writing it. So this is mostly to have it on hand. This function is needed because DateDiff doesn't check if they've had their birthday this year. CREATE FUNCTION CalculateAge ( @ Start smalldatetime , @ End smalldatetime ) RETURNS int AS BEGIN -- Declare the return variable here DECLARE @Age int SET @Age = DateDiff( year , @ Start , @ End ) IF DateAdd( year , @Age, @ Start ) > @ End SET @Age = @Age - 1 RETURN @Age END GO VB Version Public Function Age( ByVal value As Date , ByVal d As Date ) As Integer Age = DateDiff(DateInterval.Year, value, d) If value.AddYears(Age) > d Then Age = Age - 1 Return Age End Function Technorati Tags: SQL Server , Visual Basic , Age , Dates , DateDiff

Use JavaScript on Controls in a GridView, DetailsView or FormView (even in a MasterPage with an UpdatePanel)

Ever want to use JavaScript on a Textbox inside a DetailsView? It can be done, you just need to overcome two issues. The first is that .Net will change the ID from "MyTextBox" to something like "ct100_ContentPlaceholder1_DetailsView1_MyTextBox". The second is that these controls may exist only in Edit or Insert mode. The first thing we want to do is get the ID of MyTextbox that gets sent to the browser. Since MyTextbox only exists when we're editing or inserting, we need to use the PreRender event. We check to make sure we are in Edit/Insert mode and then use FindControl to get a handle on the textbox. Next we use the ScriptManager RegisterHiddenField method to register a hidden control that has the ClientID as its value. Protected Sub DetailsView1_PreRender( ByVal sender As Object , ByVal e As System.EventArgs) Handles DetailsView1.PreRender If DetailsView1.CurrentMode = DetailsViewMode.Edit Or DetailsView1.CurrentMode = DetailsViewMode.Inse...

LINQ To SQL Row not found or changed

LINQ To SQL likes to use TimeStamp columns for version tracking and makes it really easy to do so. However, in a lot of the apps I work on, we need to keep track of when the last change was made so we have a DateTime column titled LastUpdated. Using Steve Michelotti's post , I was able to use this column for row version control. Everything was fine until I was using a DetailsView and got the message "Row not found or changed". This message is LINQ's way of telling you that the row has been changed and you now have a concurrence conflict. Since I was working on my local box, I knew this wasn't the case. Pulling up SQL Profiler, I was able to see that LINQ was sending the LastUpdated date back without milliseconds. Since this isn't the same as the value on the row, it thinks a change has occurred. I was displaying the LastUpdated field in a readonly column so it shouldn't have been getting changed. I also had it as DataKey and could see that it was getti...

JavaScript DateDiff Functions

Here's some JavaScript functions that I use like VB's DateDiff function. I've seen some other functions but most take the number of days and end up close but not quite. They also don't correctly handle leap years. DateDiffYears will return the number of years between two dates while DateDiffMonths will return the number of months. Couple of things to remember. DateDiffYears is often used to determine age so it won't go negative but will return zero. Thus, if the end date precedes the start date you'll get zero as the result. Also, pass start and end as date objects rather than strings. LAstly, you need DateDiffYears to use DateDiffMonths. // Calcuates the number of years between two dates. // start - a date object that defines the beginning of the range // end - a date object that defines the end of the range function DateDiffYears(start, end) { var years = end.getFullYear() - start.getFullYear(); var month1 = start.getMonth() var month...