Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textbox focus on button when hit enter?

Tags:

asp.net

c#-4.0

This is a textbox field I have and when a user enters in a value and hit enter, I want the value to submit when the user hit enter.

<div id="divUserInfo" >
    <table align="center">
        <tr  id="trDomain" runat="server" visible="false">
            <td class="tdLabel tdLabelInfo" >Domain Name</td>
            <td class="tdData">
                <asp:TextBox ID="txtDomain" runat="server" ToolTip="Network domain user is associated."></asp:TextBox>
                <asp:Label ID="lblRqdDom" runat="server" Text="required" CssClass="noshow" ></asp:Label>
            </td>
        </tr>

Yes I know I'm formatting in a table and will not being using tables for my future projects.

like image 327
nhat Avatar asked Feb 21 '23 06:02

nhat


2 Answers

Postback on TextBox loosing focus

If you just want to do a postback when TextBox looses focus (by pressing Enter, Tab or moving focus to another control), set AutoPostBack property of the TextBox to true.

After writing something in the TextBox and making it loose focus, you'll get a postback. I suppose, however, that this can be raised not only on pressing Enter. More information on MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback.aspx.

Postback by simulating submit Button click on pressing Enter

If you want a postback only on pressing enter, you can put add a Button on the page and put it with the TextBox in the same Panel control. Then you should set DefaultButton property for this Panel. This should invoke button click on pressing Enter when the TextBox (or any other control in the Panel) is focused.

So, your code can look like this (the code hasn't been tested):

...
<asp:Panel ID="grouppingPanel" runat="server" DefaultButton="btnSubmitDomain">
    <asp:TextBox ID="txtDomain" runat="server" ToolTip="Network domain user is associated." />
    <asp:Label ID="lblRqdDom" runat="server" Text="required" CssClass="noshow" ></asp:Label>

    <asp:Button ID="btnSubmitDomain" runat="server" Text="GO" OnClick="btnSubmitDomain_Click" />
</asp:Panel>
...

You can find more information about it here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx.

If you have any issues applying any of those solutions or further questions, let me know.

like image 91
Lukasz M Avatar answered Feb 28 '23 05:02

Lukasz M


You'll either need a custom client-side event to respond to the Enter keypress to programmatically submit the form, or you'll need an input of type submit. Pressing Enter in a textbox to submit the form is the default behavior if there is a submit button on the form. If you choose to implement a custom event handler, you'll need to attach it to each textbox that should submit the form when the Enter key is pressed.

NOTE: If you have more than one submit button on the form, the default behavior is to invoke the first button as they appear in the DOM. If you don't want to display your official submit button first, you can use some CSS to rearrange the display or you can put a hidden button at the top of the form to serve as the "default action."

like image 39
TLS Avatar answered Feb 28 '23 04:02

TLS