jQueryIsReady For those still using Visual Studio 2008 and developing using jQuery, you will no doubt desire the use of IntelliSense in your text editor to browse the jQuery objects.

To make sure you get this benefit, you must have SP1 for Visual Studio 2008 applied and run a patch which you can download here.

Download jQuery 1.4.2 and the –vsdoc.js for jQuery 1.4.1 and place them in the same folder in your web app.  Rename the –vsdoc.js file to “jquery-1.4.2.min-vsdoc.js”. 

Add a script tag to the top of your ASP.NET web form or master page:

You should now be able to see IntelliSense at work anywhere you reference jQuery.


Posted in: code  Tags: ,

Very early in the life of a web form in ASP.NET, you may need to know what control fired the event that caused the post-back to occur.  Perhaps you need to know what button was clicked before the click event handler is executed.  If that scenario is the one you find yourself in, then the following code can help!

If you include the following extension method in your code, you can get the ID of the event causing control from the HttpRequest instance:

public static string GetEventTargetId(this HttpRequest req)
{
    return GetOriginalControlId(req.Form["__EVENTTARGET"]);
}// extension method

The above code is dependent on a helper method to strip the ugly nonsense potentially added while the control was rendered (can now be avoided in ASP.NET 4.0).  The helper method is defined below:

public static string GetOriginalControlId(string renderedControlId)
{
    if (renderedControlId == null) return null;
    int indexOfSeparator = renderedControlId.LastIndexOf('$');
    if (indexOfSeparator >= 0)
    {
        renderedControlId = renderedControlId.Substring(indexOfSeparator + 1);
    }
    return renderedControlId;
}// method

Posted in: code  Tags:
palermo4 posted on July 9, 2010 07:45

For those that don’t know this, you can shorthand the following script:

$(document).ready(function () {
    alert("Giddyup!");
});

… and replace the above with the following equivalent:

$(function () {
    alert("Giddyup!");
});

Posted in: code  Tags:
palermo4 posted on July 8, 2010 12:24

When I teach my introduction to C# course, I typically provide a tough numeric challenge to stimulate thinking and use all the features of the language learned up to that point.  In a recent class, I was presented the challenge of determining the last number of any integer.  For example:

  • 12 == 2
  • 337 == 7
  • 1000 == 0
  • 987654 == 4

In each example above, I needed to simply return whatever was in the “singles” place – the last number of the number.

My first approach felt like I was cheating, but it worked:

public static int GetLastNumber(string stringifiedNumber)
{
    return Convert.ToInt32(stringifiedNumber[stringifiedNumber.Length -1].ToString());
}// method

After brooding over it, I decided to challenge myself to do this mathematically instead.  Here is the final code:

public static int GetLastNumber(dynamic anyNumber)
{
    double fractional = anyNumber * .1;
    double truncated = Math.Truncate(fractional);
    return (int)(Math.Round((fractional - truncated),1) * 10);
}// method

The use of the dynamic keyword above allows me to pass in any numeric data type.

Updated: Thanks to Nicki for identifying how I over-engineered this problem.  I simply needed to get the modulus of 10 to get what I wanted.  So in one simple statement I could write:

 anyNumber % 10;

Posted in: code  Tags:
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 J. Michael Palermo IV