Hints And Tips research knowledge archive
Tip# 106
Sponsored Links
 
Subject: Make a LinkButton as default on ENTER key
Updated: Aug-2-2004
Rating: Not Rated
By: Rocky Moore - Member #: 461
Location: Klamath Falls, Oregon USA
Website: www.ReflectedThought.com
Category: Computers > Programming > .NET > ASP.NET
Rate This Tip (10=Great):  1  2  3  4  5  6  7  8  9  10
This may not be the best solutions, but it was the only one I could find after spending hours searching for an answer.  Searching MSDN for CaptureEvent, I found an article on making a hot key for a page.  With some modification, this seems to work well in making a default button.

In my code behind file I call this routine to send out Javascript that will intercept the ENTER key (13) and call a postback for the proper control.  Could also have it locate and call the onclick if you like.

To use, simply call the method passing the Web control you wish to use as the default button.

Note: This may cause a text area control that accepts ENTER to not function properly.  For me this was not an issue and I have not tested to see the results.

private void SendScript(Control SubmitButton)
{
   string buttonID = SubmitButton.UniqueID.Replace(":","$");
   string script=@"
      <script language='JavaScript' >
      <!--
         if (document.layers)
             document.captureEvents(Event.KEYPRESS);
         function checkKey(e)
         {
            if (document.layers)
            {
               if (e.which==13)
               {
	           __doPostBack('"+buttonID+@"','');
               }
            }
            else if (document.all)
            {
               if (event.keyCode==13)
               {
                   __doPostBack('"+buttonID+@"','');
               }
            }
         }

      document.onkeypress=checkKey;
      //-->
      </script>";
	
    Page.RegisterClientScriptBlock("EnterTrap",script);
}

[Submit DIGG for this tip]


-----------
Note: Use the tips posted on this site at your own risk. The tips are posted by the public and as such may or may not be valid.

Sponsored Links
     
Categories
hints and tips gold divider
Sponsor Sites To Visit
www.SaintsInLight.com
Thoughts on the Christian walk
along various Christian links.

CICK HERE
www.BringingInTheHarvest.com
Christian:  The harvest is ripe and
the day is short, we need to bring in
the harvest while there is time!

CLICK HERE
XML RSS News Feed For Recently Posted Hints And Tips  RSS Feed
You can now get updates to the Recent Tips section by using RSS. The address is:

HintsAndTips.com/Rss.aspx

* Posting Tips *
To post a Tip, Recommendation or Tips Wanted, simply browse to the category you feel is a best fit for your post (click on the Recommention or Tips Wanted tab if fitting) and then click on "New Post" option.
(C) Copyright 1998-2010 All Rights Reserved