SpriteHand
Module Border
  Submit using Enter on multiple controls
Module Border
Location: BlogsAndy's Blog    
Posted by: host 10/9/2006 6:48 PM

ASP.NET 2.0 adds the DefaultButton property, which allows you to specify which button causes a postback when the Enter key is pressed.

But what if you have multiple controls on the page that could potentially cause a postback? For example, maybe you have multiple search textboxes on your page - any of which the user should be able to press Enter on and cause a postback?

In the example below we will assume there is a textbox named "txtFind" and when the user hits <enter> on txtFind, we want to execute a Postback on btnFind.

First we add a client-side handler to the control that should cause the postback when enter is hit. You also pass in the ClientId of the element you want to cause the postback:

txtFind.Attributes.Add("onkeydown", "return OnKeyDown(event, '" + btnFind.ClientID + "');");

Then we need to add some script to handle that event in both IE and Mozilla -

    function OnKeyDown(e, submitButton)

    {

        var nKey = -1;

        var sourceElement;

 

        if (e && e.which)

            nKey = e.which;    // NS

        else

            if (window.event && window.event.keyCode)

                nKey = window.event.keyCode;  // IE

       

        if (nKey == 13)

        {

             document.getElementById(submitButton).click();

             return false;

        }

 

        return true;

    }

 

Permalink |  Trackback

Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Blog_List
Module Border
Module Border Module Border
Module Border
  Subscribe
Module Border

RSS

Module Border Module Border
Module Border
  Diversions
Module Border

DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game was submitted as an entry to the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

INFO AND CODE



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2008 andy.beaulieu.com - Login