Keypress on button

4 posts, 0 answers
  1. Fabio Cirillo
    Fabio Cirillo avatar
    180 posts
    Member since:
    Jan 2010

    Posted 04 Feb 2013 Link to this post

    hi,
    I have two buttons (button1 and button2) and for each of these buttons I have 2 textbox login. I wish that when I write in the first textbox and hit the enter button on your computer, it must start button code button1 or if I write in the second textbox and hit the enter button to start the second button button2 code .... I tried but it always starts code written in the first button. So I decided to disable the keypress event = 13 across the page. But now my client wants the buttons function by pressing the "enter" key.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Feb 2013 Link to this post

    Hi Fabio

    Try putting both buttons and textbox inside asp Panels respectively and try setting its DefaultButton property to the buttons ID
    So if once the focus comes to the first textbox and you click enter first button will hit and if the focus comes to the second textbox and you click enter the second buton gets fired.

    Thanks
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Fabio Cirillo
    Fabio Cirillo avatar
    180 posts
    Member since:
    Jan 2010

    Posted 10 Feb 2013 Link to this post

    hi,
    can you send me a example?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Feb 2013 Link to this post

    Hi,

    Try the following code to achieve your scenario.
    aspx:
    <asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </asp:Panel>
            <asp:Panel ID="Panel2" DefaultButton="Button2" runat="server">
                <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </asp:Panel>

    Thanks,
    Princy
Back to Top