Does the text box focus on the button when hit?

This is the text box field that I have, and when the user enters a value and presses enter, I want the value to be sent when the user presses the enter button.

<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 that I am formatting in a table and will not use tables for my future projects.

+4
source share
3 answers

Return to TextBox, losing focus

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

After writing something in the TextBox and removing the focus, you will receive a postback. I believe, however, that this can be enhanced not only by pressing Enter. Additional information about MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback.aspx .

Return by simulating the submit button Press the Enter button

If you want to postback only when you press enter, you can add a Button to the page and place it using the TextBox in the same Panel control. Then you must set the Panel property to DefaultButton . When you click the TextBox button (or any other control in the Panel ), when you click the button, press the "Enter" button.

So your code might look like this (code has not 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 this here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx .

If you have any problems applying any of these solutions or further questions, let me know.

+6
source

You will need a special client event to respond to Enter in order to programmatically submit the form, or you will need an input type submit . Pressing Enter in the text field to submit the form is the default behavior if the form has a submit button. If you decide to implement a special event handler, you need to attach it to each text field that the form should submit when the Enter key is pressed.

NOTE. If there is more than one submit button on the form, the default behavior is to call the first button as they appear in the DOM. If you do not want to display the official submit button first, you can use some CSS to change the display, or you can put a hidden button at the top of the form to serve as a "default action".

+1
source

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 

This method then adds an attribute for each editor control that you want to use by default:

  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 default page_load event log with each editor:

  DefaultButton(txtStreetNm, btnSearch.ClientID) DefaultButton(txtSuffix, btnSearch.ClientID) 
+1
source

Source: https://habr.com/ru/post/1399845/


All Articles