Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set the submit button focus so when user hits the enter key 1

Status
Not open for further replies.

GHOST24

Programmer
Jul 7, 2005
22
GB
I have had a look at the threads on this site but they don't make sense to me i am a complete beginner with html and java.
What i want is when the user hits the enter button it fires the submit button click event.
here is the html code can someone please show me what code to put and where to put it please.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="myloggin.aspx.vb" Inherits="WebOrder.myloggin"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>myloggin</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:image id="Image1" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 8px" runat="server"
Width="781px" ImageUrl="Title.gif" Height="67px"></asp:image><INPUT id="Submit1" style="Z-INDEX: 108; LEFT: 560px; WIDTH: 72px; POSITION: absolute; TOP: 240px; HEIGHT: 24px"
type="submit" value="Submit" name="Submit1" runat="server"><asp:label id="warnlbl" style="Z-INDEX: 107; LEFT: 648px; POSITION: absolute; TOP: 168px" runat="server"
Width="169px" Font-Names="Arial" Visible="False" ForeColor="Red" Font-Bold="True">Incorrect information</asp:label><asp:label id="Label2" style="Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 197px" runat="server"
Width="149px" Font-Names="Arial">Password</asp:label><asp:label id="Label1" style="Z-INDEX: 104; LEFT: 288px; POSITION: absolute; TOP: 152px" runat="server"
Width="149px" Font-Names="Arial">Select Retailer Code</asp:label><asp:textbox id="pwd" style="Z-INDEX: 103; LEFT: 456px; POSITION: absolute; TOP: 192px" tabIndex="2"
runat="server" Width="170px" Font-Names="Arial" TextMode="Password"></asp:textbox><asp:dropdownlist id="uidddl" style="Z-INDEX: 102; LEFT: 456px; POSITION: absolute; TOP: 152px" tabIndex="1"
runat="server" Width="170px" Font-Names="Arial"></asp:dropdownlist></form>
</body>
</HTML>
 
>>What i want is when the user hits the enter button it fires the submit button click event.


that happens automatically when a "Submit" button is there in a form. Where exactly do you want this event?


anyway info on how to do it (in any conrol):

onkeydown="event.keyCode==13?document.forms[0].submit();''">

Known is handfull, Unknown is worldfull
 
The page is a login page the user selects is code from a drop down list that is populated by sql, He then enters a password and click the submit button if he hits the entre key it removes the password from he password box.
there are three lables, a dropdown box and a submit button on the page.
 
hi vbkris,
I had the same problem. I was wondering if it possible to handle kexboard events within the form behind: something like that:

sub keyEnterDow (obj as object, e as eventargs)
actions
end sub

it seems it is not... and that I need to use javascript in order to trigger a subroutine with the enter key. If I click the enter key in a normal aspx page it would only cause a postback.

the code you posted is it javascript? I added it to my textbox but it didn't work...
 
it is javascript, can i have ur textbox code?

Known is handfull, Unknown is worldfull
 
on button click the program checks the password against the sql table and returns a result of 0,1,2 or 3

' test to see if password is filled in
Dim intresult As Integer
If Not pwd.Text = "" Then

'code here will try the password against the user id
SqlCommand4.Parameters("@username").Value = uidddl.SelectedItem.Text
SqlCommand4.Parameters("@password").Value = pwd.Text
SqlConnection1.Open()
SqlCommand4.ExecuteNonQuery()
intresult = SqlCommand4.Parameters("@return_value").Value
SqlConnection1.Close()

Select Case intresult
Case 0
Call selectaddress()
Response.Redirect("index2.aspx")

Case 1
warnlbl.Visible = True
warnlbl.Text = "userid"
Case 2
warnlbl.Visible = True
Case 3
'admin password
Call selectaddress()
Response.Redirect("index.aspx")
End Select
End If
 
There are two quite simple ways of creating a default button for the page:

1) In the page load event add the following (change the name of btnSearch to the relevant asp button):
Code:
Page.RegisterHiddenField("__EVENTTARGET", "btnSearch")

2) Use javascript. e.g.
Code:
<script language="javascript">
    function KeyDownHandler(btn)
    {
        // process only the Enter key
        if (event.keyCode == 13)
        {
            // cancel the default submit
            event.returnValue=false;
            event.cancel = true;
            // submit the form by programmatically clicking the specified button
            btn.click();
        }
    }
</script>

<asp:Button Runat="server" ID="DefButton" />
<asp:TextBox Runat="server" ID="FirstName" onKeyDown= _
    "KeyDownHandler(DefButton)" />


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Like I said I'm a complete beginner so when you say the page load is this in the vb code or in the html?
and at which point to i put the <script guage="javascript"> ?

 
If you go with option 1, then you need to add it in the page load event of the actual page e.g.
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Page.RegisterHiddenField("__EVENTTARGET", "btnSearch")
End Sub
If you go with option 2, simply add it to the html as it is javascript.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top