enter-key works with two inputboxes but not with olny one.

  • Thread starter Thread starter Phil
  • Start date Start date
P

Phil

Hi,

There is a form with two inputboxes and a submit button.The focus is on the
first inputbox.
When the user presses the enter-key without having entered some text, the
javascript message "wrong text" appears. This works.

But if i remove the second inputbox, leaving only the first inputbox and the
submit button, pressing the enter-key without any text does not show the
javascript message. Nothing happens. The focus remains in the inputbox. You
can see the page is postbacked, but that's all.

Why does it work with two inputboxes and not with one? And how to fix it?
Thanks
Phil


<body><form id="form1" runat="server">
<table>
<tr><td><input id="Text1" type="text" runat="server" /></td></tr>
<tr><td><input id="Text2" type="text" runat="server" /></td></tr>
<tr><td><input id="Submit1" type="submit" runat="server"
/></td></tr></table>
</form></body>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Text1.Focus()
End Sub

Protected Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Submit1.ServerClick
Dim beh As String
beh = Text1.value
If beh = "xxx" Then
Response.Redirect("start.aspx", False)
Else
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(),
"myscript", _
" alert('wrong text');", True)
End If
End Sub
 
Hi,

There is a form with two inputboxes and a submit button.The focus is on the
first inputbox.
When the user presses the enter-key without having entered some text, the
javascript message "wrong text" appears. This works.

But if i remove the second inputbox, leaving only the first inputbox and the
submit button, pressing the enter-key without any text does not show the
javascript message. Nothing happens. The focus remains in the inputbox. You
can see the page is postbacked, but that's all.

Why does it work with two inputboxes and not with one? And how to fix it?
Thanks
Phil

<body><form id="form1" runat="server">
<table>
 <tr><td><input id="Text1" type="text" runat="server" /></td></tr>
 <tr><td><input id="Text2" type="text"  runat="server" /></td></tr>
<tr><td><input id="Submit1" type="submit" runat="server"
/></td></tr></table>
</form></body>

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
 Text1.Focus()
 End Sub

Protected Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Submit1.ServerClick
        Dim beh As String
        beh = Text1.value
        If beh = "xxx" Then
             Response.Redirect("start.aspx", False)
        Else
            Page.ClientScript.RegisterClientScriptBlock(Me.GetType(),
"myscript", _
            " alert('wrong text');", True)
        End If
End Sub

I think that you culd get better results using aspnet's validation
controls. You'll get validation on client side, among other benefits.
http://msdn.microsoft.com/en-us/library/aa479013.aspx
Best regards,
 
Back
Top