RegisterHiddenField to Capture Enter Resetting Application

Discussion in 'ASP .Net' started by Matthew Wieder, Jul 18, 2003.

  1. 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","SomeButtonOnThePage");
    Matthew Wieder, Jul 18, 2003
    #1
    1. Advertising

  2. 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: <>
    !Date: Fri, 18 Jul 2003 09:08:18 -0400
    !From: Matthew Wieder <>
    !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!TK2MSFTNGP12.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","SomeButtonOnThePage");
    ! .
    ! .
    !
    !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!
    !
    !
    Yan-Hong Huang[MSFT], Jul 22, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MeDhanush
    Replies:
    1
    Views:
    910
    Natty Gur
    Aug 28, 2003
  2. Kevin Spencer

    RegisterHiddenField and trace

    Kevin Spencer, Oct 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,078
    Erik Cruz
    Oct 22, 2003
  3. Francis
    Replies:
    0
    Views:
    671
    Francis
    Nov 3, 2003
  4. Mario D'Alessio

    Regex resetting the capture buffer

    Mario D'Alessio, Jun 21, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    272
    Mario D'Alessio
    Jun 22, 2007
  5. HopfZ
    Replies:
    0
    Views:
    562
    HopfZ
    Aug 28, 2006
Loading...

Share This Page