By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,023 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,023 IT Pros & Developers. It's quick & easy.

RegisterHiddenField to Capture Enter Resetting Application

P: n/a
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(object sender, System.EventArgs e)
{
Page.RegisterHiddenField("__EVENTTARGET","SomeButt onOnThePage");
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.Attributes("OnKeypress") = "return KeyPressFunction('button1')"
txtText2.Attributes("OnKeypress") = "return KeyPressFunction('button2')"

Then add the following code in the .aspx page

<script language="javascript">
function KeyPressFunction(buttonName)
{
if (event.keyCode == 13)
{
if (buttonName == "button1")
{
Form1.button1.click();
return false;
}

if (buttonName == "button2")
{
Form1.button2.click();
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...regardless 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**************@SatoriGroupInc.com>
!Date: Fri, 18 Jul 2003 09:08:18 -0400
!From: Matthew Wieder <De*********@SatoriGroupInc.com>
!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: RegisterHiddenField to Capture Enter Resetting Application
!Content-Type: text/plain; charset=us-ascii; format=flowed
!Content-Transfer-Encoding: 7bit
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!NNTP-Posting-Host: 207.106.112.178
!Lines: 1
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:160248
!X-Tomcat-NG: microsoft.public.dotnet.framework.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(object sender, System.EventArgs e)
!{
! Page.RegisterHiddenField("__EVENTTARGET","SomeButt onOnThePage");
! .
! .
!
!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!
!
!
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.