Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 20th, 2006, 04:39 AM
Authorized User
 
Join Date: Jan 2006
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to set Default Button to "enter Key"

hai every one ,
let me know that can any one help me to go tho it.
i had a asp.net webform in which i am using four buttons.
when i click enter we are seeing all validations which are for the third button button i want this not happen because i want first button "search" to take place as when the focus of the mouse is there and also i want to set the default to second button ie "advanced search" i am using asp.net 1.1 so let me solve this in this version .

thank in advance for one and all
__________________
Regards,
P.Madhusudhana Rao
 
Old March 16th, 2006, 02:31 AM
Registered User
 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hitting the enter key in a TextBox can sometimes have undesired effects like the wrong submit Button being “clicked“. The method described below allows you to specify a default Button to submit when the user hits the enter key in a TextBox.

When you press a key on your keyboard, the js OnKeyPress event is fired. This calls a function to which we pass the id of the button associated with the TextBox. The function gets a reference to the button and simuilates a mouse-click on the Button. We perform a browser detection because IE and Netscape have different event models. The function finally returns false so that the keypress event is cancelled (otherwise a second form submit will be raised). This works with newer versions of IE/Netscape.

//client side js
function clickButton(e, buttonid){
      var bt = document.getElementById(buttonid);
      if (typeof bt == 'object'){
            if(navigator.appName.indexOf("Netscape")>(-1)){
                  if (e.keyCode == 13){
                        bt.click();
                        return false;
                  }
            }
            if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){
                  if (event.keyCode == 13){
                        bt.click();
                        return false;
                  }
            }
      }
}



//code behind
TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");

The code behind generates the following code:

<input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'Button1')" />

This causes web control Button1 to be clicked when the enter key is hit inside TextBox1.

This can easily be extended to support other browsers either by browser detection or object detection.



 
Old April 10th, 2006, 10:42 AM
Registered User
 
Join Date: Apr 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm sorry to resurrect an old topic, but I've got a Microsoft Access Data Access Page, and I need to accomplish the same thing. Is there an equivalent to your code for the type of pages that Access creates?





Similar Threads
Thread Thread Starter Forum Replies Last Post
enter key should execute button click event MunishBhatia ASP.NET 2.0 Professional 6 November 25th, 2007 10:59 PM
enter key on textbox, performs button event code?? squeakstar Visual Basic 2005 Basics 4 June 22nd, 2006 05:13 AM
how to disable enter key for a particular button g_vamsi_krish ASP.NET 1.0 and 1.1 Professional 1 January 19th, 2006 11:22 AM
Enter key vs Click Button akibaMaila VB.NET 2002/2003 Basics 1 January 14th, 2005 12:49 PM
Button Focus and Enter Key nigel VS.NET 2002/2003 1 August 19th, 2003 07:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.