hitting enter button while in textbox calls correct button's click event

Discussion in 'ASP .Net' started by TS, Nov 23, 2005.

  1. TS

    TS Guest

    for some reason, it posts to the server, but no click events of any buttons
    on form fire. the button is the first one on the form. when the focus is
    inside the textbox, it doesnt' work. if i click the form, it works fine.

    how do i make it fire the click event of this button? (i have it on a diff.
    page and it works fine...can't figure out why it is diff. here)

    thanks to all!

    (i tried tab order)
    TS, Nov 23, 2005
    #1
    1. Advertising

  2. Hi TS,

    Thanks for your post.

    Based on my understanding, you want to get the function that: when the user
    hits enter key in the textbox, just trigger the server side Button's Click
    event.

    To achieve this, we have to do some customization at the client side. We
    can use javascript to cancel the current enter key press operation, then
    programmatically invoke the button.click() method. Also, we can first
    associate the client textbox.onkeypress event with the client javascript
    code. Below is the demo code:

    private void Page_Load(object sender, System.EventArgs e)
    {
    this.TextBox1.Attributes.Add("onkeypress","button_click()");
    }

    <script language="javascript">
    function button_click()
    {
    if(window.event.keyCode==13)
    {
    var Button1=document.getElementById("Button1");
    Button1.click();
    window.event.cancel=true;
    }
    }
    </script>

    This code works well on my side. Hope it helps

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Nov 24, 2005
    #2
    1. Advertising

  3. RE: hitting enter button while in textbox calls correct button's c

    Thanks i will try that, i'm sure it will work.

    Can you tell me though why this is occuring so i can understand the root
    cause? Like I said, i have a different page that when I hit enter while
    cursor in the textbox, it correctly posts to the server and calls the server
    side click event handler and works how i want it, and i'm wondering why it
    works there, and not on my other page.

    thanks a bunch!

    ""Jeffrey Tan[MSFT]"" wrote:

    > Hi TS,
    >
    > Thanks for your post.
    >
    > Based on my understanding, you want to get the function that: when the user
    > hits enter key in the textbox, just trigger the server side Button's Click
    > event.
    >
    > To achieve this, we have to do some customization at the client side. We
    > can use javascript to cancel the current enter key press operation, then
    > programmatically invoke the button.click() method. Also, we can first
    > associate the client textbox.onkeypress event with the client javascript
    > code. Below is the demo code:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > this.TextBox1.Attributes.Add("onkeypress","button_click()");
    > }
    >
    > <script language="javascript">
    > function button_click()
    > {
    > if(window.event.keyCode==13)
    > {
    > var Button1=document.getElementById("Button1");
    > Button1.click();
    > window.event.cancel=true;
    > }
    > }
    > </script>
    >
    > This code works well on my side. Hope it helps
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
    >
    =?Utf-8?B?VFM=?=, Nov 28, 2005
    #3
  4. TS

    TS Guest

    Re: hitting enter button while in textbox calls correct button's c

    Also, why does it post to the server at all? it would seem that if it posts
    to the server, then obvioulsy the submit button was clicked, or was a
    different event evoked that didn't have an event handler?


    "TS" <> wrote in message
    news:...
    > Thanks i will try that, i'm sure it will work.
    >
    > Can you tell me though why this is occuring so i can understand the root
    > cause? Like I said, i have a different page that when I hit enter while
    > cursor in the textbox, it correctly posts to the server and calls the
    > server
    > side click event handler and works how i want it, and i'm wondering why it
    > works there, and not on my other page.
    >
    > thanks a bunch!
    >
    > ""Jeffrey Tan[MSFT]"" wrote:
    >
    >> Hi TS,
    >>
    >> Thanks for your post.
    >>
    >> Based on my understanding, you want to get the function that: when the
    >> user
    >> hits enter key in the textbox, just trigger the server side Button's
    >> Click
    >> event.
    >>
    >> To achieve this, we have to do some customization at the client side. We
    >> can use javascript to cancel the current enter key press operation, then
    >> programmatically invoke the button.click() method. Also, we can first
    >> associate the client textbox.onkeypress event with the client javascript
    >> code. Below is the demo code:
    >>
    >> private void Page_Load(object sender, System.EventArgs e)
    >> {
    >> this.TextBox1.Attributes.Add("onkeypress","button_click()");
    >> }
    >>
    >> <script language="javascript">
    >> function button_click()
    >> {
    >> if(window.event.keyCode==13)
    >> {
    >> var Button1=document.getElementById("Button1");
    >> Button1.click();
    >> window.event.cancel=true;
    >> }
    >> }
    >> </script>
    >>
    >> This code works well on my side. Hope it helps
    >>
    >> Best regards,
    >> Jeffrey Tan
    >> Microsoft Online Partner Support
    >> Get Secure! - www.microsoft.com/security
    >> This posting is provided "as is" with no warranties and confers no
    >> rights.
    >>
    >>
    TS, Nov 28, 2005
    #4
  5. TS

    TS Guest

    Re: hitting enter button while in textbox calls correct button's c

    More info:

    I am setting the focus to the textbox on page load so the user can
    immediately type in a number they want to search and then hit enter key to
    submit. Again this is the exact same steps used on the other page that
    functions correctly.

    thanks again

    "TS" <> wrote in message
    news:...
    > Thanks i will try that, i'm sure it will work.
    >
    > Can you tell me though why this is occuring so i can understand the root
    > cause? Like I said, i have a different page that when I hit enter while
    > cursor in the textbox, it correctly posts to the server and calls the
    > server
    > side click event handler and works how i want it, and i'm wondering why it
    > works there, and not on my other page.
    >
    > thanks a bunch!
    >
    > ""Jeffrey Tan[MSFT]"" wrote:
    >
    >> Hi TS,
    >>
    >> Thanks for your post.
    >>
    >> Based on my understanding, you want to get the function that: when the
    >> user
    >> hits enter key in the textbox, just trigger the server side Button's
    >> Click
    >> event.
    >>
    >> To achieve this, we have to do some customization at the client side. We
    >> can use javascript to cancel the current enter key press operation, then
    >> programmatically invoke the button.click() method. Also, we can first
    >> associate the client textbox.onkeypress event with the client javascript
    >> code. Below is the demo code:
    >>
    >> private void Page_Load(object sender, System.EventArgs e)
    >> {
    >> this.TextBox1.Attributes.Add("onkeypress","button_click()");
    >> }
    >>
    >> <script language="javascript">
    >> function button_click()
    >> {
    >> if(window.event.keyCode==13)
    >> {
    >> var Button1=document.getElementById("Button1");
    >> Button1.click();
    >> window.event.cancel=true;
    >> }
    >> }
    >> </script>
    >>
    >> This code works well on my side. Hope it helps
    >>
    >> Best regards,
    >> Jeffrey Tan
    >> Microsoft Online Partner Support
    >> Get Secure! - www.microsoft.com/security
    >> This posting is provided "as is" with no warranties and confers no
    >> rights.
    >>
    >>
    TS, Nov 28, 2005
    #5
  6. Re: hitting enter button while in textbox calls correct button's c

    Hi TS,

    This behavior is client browser specific, which do not have a definit way.
    I think "Steve C. Orr [MVP, MCSD]" has provided you an informative reply
    in "how do i determine what event caused postback" post. Please check it
    there. Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Nov 29, 2005
    #6
    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. Nate Hekman

    Hitting Enter in a textbox

    Nate Hekman, Sep 23, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    496
    Matt Berther
    Sep 23, 2004
  2. Jie
    Replies:
    7
    Views:
    6,140
    Xavier Pacheco
    Oct 9, 2004
  3. Replies:
    4
    Views:
    3,773
  4. Paul Furman
    Replies:
    4
    Views:
    1,027
    Paul Furman
    Mar 17, 2007
  5. Sunfire
    Replies:
    2
    Views:
    1,056
    Sunfire
    Nov 6, 2007
Loading...

Share This Page