4

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.

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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."

Comments

1

First, register this javascript in your page_load event:

    Public Shared Sub jsTrapEnter(ByRef p_Page As System.Web.UI.Page)

    Dim sScript As New System.Text.StringBuilder

    sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
    sScript.Append("function fnTrapKD(btn){" & vbCrLf)
    sScript.Append(" if (document.all){" & vbCrLf)
    sScript.Append("   if (event.keyCode == 13)" & vbCrLf)
    sScript.Append("   { " & vbCrLf)
    sScript.Append("     event.returnValue=false;" & vbCrLf)
    sScript.Append("     event.cancel = true;" & vbCrLf)
    sScript.Append("     btn.click();" & vbCrLf)
    sScript.Append("   } " & vbCrLf)
    sScript.Append(" } " & vbCrLf)
    sScript.Append("}" & vbCrLf)
    sScript.Append("</SCRIPT>" & vbCrLf)

    If Not p_Page.ClientScript.IsClientScriptBlockRegistered("ForceDefaultToScript") Then
        p_Page.ClientScript.RegisterClientScriptBlock(p_Page.GetType, "ForceDefaultToScript", sScript.ToString)
    End If

End Sub

Then this method adds an attribute to each editor control that you want to have a default button:

    Public Shared Sub DefaultButton(ByVal objTextControl As Web.UI.WebControls.WebControl, ByVal objDefaultButtonID As String)

    objTextControl.Attributes.Add("onkeydown", String.Format("fnTrapKD(document.all.{0})", objDefaultButtonID))

End Sub

Then in our page_load event register the default button with each editor:

            DefaultButton(txtStreetNm, btnSearch.ClientID)
        DefaultButton(txtSuffix, btnSearch.ClientID)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.