Results 1 to 4 of 4

Thread: [RESOLVED] Enter Key on Textfield

  1. #1

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Resolved [RESOLVED] Enter Key on Textfield

    Hey there,

    i have created a web form and on it are 3 read only textfields, 1 normal textfield and 4 buttons

    When data is entered in the textfield i want to be able to call one of the buttons when they hit enter after typing in the data in the textfield.

    How could i get this to work
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  2. #2
    Member Nikhil Aggarwal's Avatar
    Join Date
    Jun 2005
    Location
    New Delhi, India
    Posts
    36

    Re: Enter Key on Textfield

    C# Code:
    Clicking enter in a textbox would normally call the default button on the page.
    To override this you need to add some server side code
    Code:
    protected override void OnPreRender(EventArgs e)
    {
         base.OnPreRender (e);
         TextBox1.Attributes.Add("onkeydown",string.Format("if(window.event.keyCode == 13) {{{0}.focus();}}",Button1.ClientID));
    }
    VB Code:
    1. Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
    2.      TextBox1.Attributes.Add("onkeydown", String.Format("if(window.event.keyCode == 13) {{{0}.focus();}}", Button1.ClientID))
    3. End Sub
    Note: Dont forget to replace "TextBox1" with the name of your textbox and "Button1" with the name of the button you want to call.
    Nikhil Aggarwal

  3. #3

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: Enter Key on Textfield

    whats the default button on the page.... could i even set this

    that code worked great...

    what exactly does the prerender event do
    Last edited by Strider; Jun 23rd, 2005 at 08:20 AM.
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  4. #4
    Member Nikhil Aggarwal's Avatar
    Join Date
    Jun 2005
    Location
    New Delhi, India
    Posts
    36

    Re: Enter Key on Textfield

    Forgive me for my incorrect use of terminology - but there is no such thing as a default button for the page.
    However I've noticed a very intresting thing:
    I took a blank page and added a text box to it. During runtime when I type something in the textbox and press "Enter" the page posts back.
    Now I add a button to the page. Again during runtime the same thing happens.
    Next I added a second text box to the page. Now when I press enter in any of the textboxes, the page posts back and the Click event of the Button is fired.
    Now add a second button to the page. Pressing Enter in any of the textboxes still fires the click event of the first button but not for the second button.

    Does anyone know why?
    Nikhil Aggarwal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width