Find out which Key was pressed using jQuery

A user had a requirement to take actions on a page based on the KeyPressed. Here’s a simple script that you can do that and works cross-browser.

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<
title>Find KeyPressed</title>

<
script src="http://code.jquery.com/jquery-latest.js"
type="text/javascript"></script>
<
script type="text/javascript">
$(document).ready(function() {
$(this).bind('keypress', function(e) {
var key = (e.keyCode ? e.keyCode : e.charCode);
alert(key);
});
});
</script>
</
head>
<
body>
<
div>
</
div>
</
body>
</
html>



You can also use ‘e.which’ instead of ‘e.charCode’. I have tested the code on IE7, Chrome and Firefox 3.

To do the same on an ASP.NET page, check my article over here Implementing KeyBoard Shortcuts in ASP.NET applications using jQuery

How to add CSS Programmatically to an ASP.NET Page

I recently wrote an article on How to add a CSS Link programmatically using JavaScript. A user asked me how to do it in an ASP.NET page. It’s quite simple actually using the HtmlLink class which gives you programmatic access to the HTML <link> element.

Start by adding a reference to the System.Web.UI.HtmlControls namespace. Also make sure that your head tag has the runat=”server” attribute

<head runat="server">
<
title>Add CSS Dynamically</title>
</
head>

Then add the following code in your code behind.

C#

protected void Page_Init(object sender, EventArgs e)
{
HtmlLink link = new HtmlLink();
link.Href = "~CSS/FloatDes.css";
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(link);
}

VB.NET

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Dim link As New HtmlLink()
link.Href = "~CSS/FloatDes.css"
link.Attributes.Add("rel", "stylesheet")
link.Attributes.Add("type", "text/css")
Page.Header.Controls.Add(link)
End Sub

Now run your page and check it’s source. You will see the link to the CSS file added in the head element as shown below:

<head><title>

Add CSS Dynamically

</title><link href="~CSS/FloatDes.css" rel="stylesheet" type="text/css" /></head>

Expression Studio 3 Released – Some Important Links

The wait ends. Expression Studio 3 is now available for a Free 60 days trial.

Microsoft Expression Studio opens up a new world of creative possibility. Its professional design tools give you the freedom to make your vision real—whether you’re designing for standards-based Web sites, rich desktop experiences, or Silverlight. Includes Expression Web, Expression Blend, Expression Design and Expression Encoder. The Expression Studio 3 Suite consists of the following products

  • Expression Blend™ 3
  • SketchFlow
  • Expression Web 3
  • Expression Design 3
  • Expression Encoder 3

    Note: Expression Blend 3 was released as a trial some time ago.

    Here are some important links:

  • Top 10 Features of Expression Studio 3

  • Expression Studio 3 Free Trial – Links to all the products in the Expression 3 Suite

  • Expression Studio 3 Pre-Order

  • Expression Studio 3 Overview Video

  • How to add a CSS Link programmatically using JavaScript

    In one of my earlier posts, I demonstrated how to Programmatically Retrieve StyleSheet Url’s Referenced in your Page using JavaScript. In this post, here’s a piece of JavaScript code that demonstrates how you can programmatically reference a CSS link on a page.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    title></title>
    <
    link href="CSS/FloatDes.css" rel="stylesheet"
    title="Float layout" type="text/css" />

    <
    script type="text/javascript">
    function
    addCSS() {
    var headtg = document.getElementsByTagName('head')[0];
    if (!headtg) {
    return;
    }
    var linktg = document.createElement('link');
    linktg.type = 'text/css';
    linktg.rel = 'stylesheet';
    linktg.href = 'CSS/RoundCorners.css';
    linktg.title = 'Rounded Corners';
    headtg.appendChild(linktg);
    }
    </script>
    </
    head>
    <
    body onload="addCSS()">

    </
    body>
    </
    html>


    The code shown above creates a link element(with properties) and appends it as a child to the &lt;head&gt; section. You can use this technique to let users choose styles and change the appearance of the page.

    Programmatically Retrieve StyleSheet Url’s Referenced in your Page using JavaScript

    I came across a requirement where the stylesheets referenced by a page had to be listed. The user was then given the ability to disable the stylesheet he did not want. Here’s how we can use the document.styleSheets collection to retrieve the collection of styleSheet objects that are referenced using a &lt;link&gt; and then disable the stylesheet the user does not desire. A portion of the code is as given here:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    title></title>
    <
    link href="CSS/FloatDes.css" rel="stylesheet"
    title="Float layout" type="text/css" />
    <
    link href="CSS/RoundCorners.css" rel="stylesheet"
    title="Rounded Corners" type="text/css" />

    <
    script type="text/javascript">
    for
    (i = 0; i < document.styleSheets.length; i++) {
    alert(document.styleSheets[i].title);
    alert(document.styleSheets[i].href);
    }
    </script>
    </
    head>
    <
    body>

    </
    body>
    </
    html>



    Similarly, if you have imported stylesheets using @import, you can access the style sheets using document.styleSheets[i].imports.

    To disable a stylesheet programmatically, use this piece of code:

    document.styleSheets[i].disabled = true;

    To check if the styleSheet collection is supported on your browser, use this :

     if (typeof document.styleSheets != "undefined")



    This code has been tested on IE7 and Firefox 3

    FireFox 3.5.1 has been released

    FireFox 3.5.1 was released on July 16th 2009 to fix several security and stability issues. One of the major bug fixes was for a security issue with the software's browser's TraceMonkey JavaScript engine. Mozilla is one of its comments had stated "Just-In-Time" (JIT) compiler used for JavaScript. A crash could result in an exploitable memory corruption problem that could, under certain cases, be exploited by an attacker to run arbitrary code, like malware”. This release also speeds up launch time on some Windows systems.

    Here are some important links for this release

    Security Issues fixed in FireFox 3.5.1

    List of changes in FireFox 3.5.1

    Firefox 3.5 release notes

    The latest version of FireFox can be downloaded over here

    Sort Items in an ASP.NET DropDownList using LINQ

    Have you been looking out for a way to sort the items of an ASP.NET DropDownList using LINQ? Here’s a LINQ solution:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <
    head runat="server">
    <
    title>Sorting DropDownList Items</title>
    </
    head>
    <
    body>
    <
    form id="form1" runat="server">
    <
    div>
    <
    asp:DropDownList ID="DropDownList1" runat="server">
    <
    asp:ListItem Text="Item3"></asp:ListItem>
    <
    asp:ListItem Text="Item1"></asp:ListItem>
    <
    asp:ListItem Text="Item4"></asp:ListItem>
    <
    asp:ListItem Text="Item5"></asp:ListItem>
    <
    asp:ListItem Text="Item2"></asp:ListItem>
    </
    asp:DropDownList>
    <
    br />
    <
    asp:Button ID="btnSort" runat="server" Text="Sort"
    onclick="btnSort_Click"/>
    </
    div>
    </
    form>
    </
    body>
    </
    html>



    C#

    protected void btnSort_Click(object sender, EventArgs e)
    {
    List<string> listItems =
    DropDownList1.Items.Cast<ListItem>().Select(item => item.Text).ToList();
    listItems.Sort((a, b) => string.Compare(a, b));
    DropDownList1.DataSource = listItems;
    DropDownList1.DataBind();
    }

    VB.NET

    Protected Sub btnSort_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim listItems As List(Of String) = _
    DDL.Items.Cast(Of ListItem)().Select(Function(item) item.Text).ToList()
    listItems.Sort(Function(a, b) String.Compare(a, b))
    DDL.DataSource = listItems
    DDL.DataBind()
    End Sub

    Before Sorting

    image

    After Sorting

    image

    Search an ASP.NET DropDownList

    A user mailed me recently asking me the simplest way to search an ASP.NET DropDownList. I think the simplest way is using the foreach loop as shown below (although it may be slow when the items are huge):

    <html xmlns="http://www.w3.org/1999/xhtml">
    <
    head runat="server">
    <
    title></title>
    </
    head>
    <
    body>
    <
    form id="form1" runat="server">
    <
    div>
    <
    asp:DropDownList ID="DropDownList1" runat="server">
    <
    asp:ListItem Text="Item1"></asp:ListItem>
    <
    asp:ListItem Text="Item2"></asp:ListItem>
    <
    asp:ListItem Text="Item3"></asp:ListItem>
    </
    asp:DropDownList>
    <
    br />
    <
    asp:Button ID="btnSearch" runat="server" Text="Search"
    onclick="btnSearch_Click"/>
    </
    div>
    </
    form>
    </
    body>
    </
    html>

    C#

    protected void btnSearch_Click(object sender, EventArgs e)
    {
    string str = "Item2";
    // Using For Each
    foreach (ListItem li in DropDownList1.Items)
    {
    if (li.Value == str)
    {
    this.DropDownList1.SelectedValue = li.Value.ToLower();

    }
    }
    }

    VB.NET

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim str As String = "Item2"
    ' Using For Each
    For Each li As ListItem In DropDownList1.Items
    If li.Value = str Then
    Me
    .DropDownList1.SelectedValue = li.Value.ToLower()

    End If
    Next
    li
    End Sub
    The code shown above loops through all the items of a DropDownList and selects the item if the string is found. To make the code case insensitive, use ToLower()/ToUpper() and compare the values.