- Make a form post by an enter button?

Results 1 to 2 of 2

Thread: - Make a form post by an enter button?

  1. #1
    Join Date
    Dec 1969

    Default - Make a form post by an enter button?

    I’ve run into a bit of a problem. I am developing a lot of web forms that have text boxes and buttons that do various things like searching data or submitting information. However, I can’t seem to create a textbox server control that will perform a post on the form that it is in when the enter key is pressed at a time when it is focused with the cursor inside of it. I can’t remember ever having this problem before but I also can’t remember ever trying it. I do have a solution that catches a keyboard event with a lot of javascript, i.e:<BR><BR> <BR><BR>Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<BR><BR> &#039;CODEGEN: This method call is required by the Web Form Designer<BR><BR> &#039;Do not modify it using the code editor.<BR><BR> InitializeComponent()<BR><BR> Dim sScript As New System.Text.StringBuilder()<BR><BR> <BR><BR> sScript.Append("&#060;SCRIPT language=""javascript""&#062;" & vbCrLf)<BR><BR> sScript.Append("function fnTrapKD(btn){" & vbCrLf)<BR><BR> sScript.Append(" if (document.all){" & vbCrLf)<BR><BR> sScript.Append(" if (event.keyCode == 13)" & vbCrLf)<BR><BR> sScript.Append(" { " & vbCrLf)<BR><BR> sScript.Append(" event.returnValue=false;" & vbCrLf)<BR><BR> sScript.Append(" event.cancel = true;" & vbCrLf)<BR><BR> sScript.Append(";" & vbCrLf)<BR><BR> sScript.Append(" } " & vbCrLf)<BR><BR> sScript.Append(" } " & vbCrLf)<BR><BR> sScript.Append("}" & vbCrLf)<BR><BR> sScript.Append("&#060;/SCRIPT&#062;" & vbCrLf)<BR><BR> <BR><BR> TextBox1.Attributes.Add("onkeydown", "fnTrapKD(document.all." & Button1.ClientID & ")")<BR><BR> Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)<BR><BR>End Sub<BR><BR> <BR><BR> <BR><BR>Do you know of an easier/better way?<BR><BR> <BR><BR>Thanks for your help.<BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default For future reference

    you may want to try posting this in the ASP.NET forum!

Posting Permissions

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