Hello Matthew,
If we have multiple buttons on a webform, the first one rendered in the browser will fire when you press the Enter button.
In this scenario, you need to add attributes to textboxes in the Page_Load event to check which Button_click event to fire.
txtText1.Attrib utes("OnKeypres s") = "return KeyPressFunctio n('button1')"
txtText2.Attrib utes("OnKeypres s") = "return KeyPressFunctio n('button2')"
Then add the following code in the .aspx page
<script language="javas cript">
function KeyPressFunctio n(buttonName)
{
if (event.keyCode == 13)
{
if (buttonName == "button1")
{
Form1.button1.c lick();
return false;
}
if (buttonName == "button2")
{
Form1.button2.c lick();
return false;
}
}
}
</script>
Summary: If you are typing in txtText1 and hit Enter, button1_click event will fire, If you are typing in txtText2 and hit Enter,
button2_click event will fire...regardle ss of what other buttons are on the page.
Thanks.
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
!Message-ID: <3F************ **@SatoriGroupI nc.com>
!Date: Fri, 18 Jul 2003 09:08:18 -0400
!From: Matthew Wieder <De*********@Sa toriGroupInc.co m>
!User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02
!X-Accept-Language: en-us, en, he
!MIME-Version: 1.0
!Subject: RegisterHiddenF ield to Capture Enter Resetting Application
!Content-Type: text/plain; charset=us-ascii; format=flowed
!Content-Transfer-Encoding: 7bit
!Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
!NNTP-Posting-Host: 207.106.112.178
!Lines: 1
!Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
!Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1602 48
!X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
!
!Hi - I wanted to capture the enter button on a form since I have a
!datagrid with the first column being a delete button and if someone hits
!enter it deletes the first record. I coded:
!
!private void Page_Load(objec t sender, System.EventArg s e)
!{
! Page.RegisterHi ddenField("__EV ENTTARGET","Som eButtonOnThePag e");
! .
! .
!
!but now when I hit enter it goes back to the first web page in the
!application! Does anyone know either what is causing this behaviour, or
!how I can just "eat" the enter button so it doesn't do anything?
!thanks!
!
!