Default Button

  • Thread starter Thread starter Michael Albanese
  • Start date Start date
M

Michael Albanese

I recently read an article that mentioned adding code to
the onload event to "register a default button".

The behavior i am after is that when the user hits enter,
the form submits.

Does anyone know how to do this using vb & asp.net?

Thanks
 
If you have a submit button on the form, when the user
hits enter, it should submit the form

Tu-Thach
 
OK, but say that i have a login form where the button_click event runs
code that calls a stored procedure. My form will not submit when I hit
the enter key. Should i place the code in an "on_submit " event or
something?


Here is the HTML and the code behind of the login form

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="Login.aspx.vb" Inherits="OSHA.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>OSHA Reporting Login</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="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<LINK href="Styles.css" type="text/css" rel="stylesheet">
</HEAD>
<body bgColor="#ffffff" MS_POSITIONING="GridLayout">
<form id="frmLogin" method="post" runat="server">
<asp:label id="lblInstructions" style="Z-INDEX: 101; LEFT: 119px;
POSITION: absolute; TOP: 34px"
runat="server" Width="550px" Height="146px" BackColor="Lavender"
BorderColor="Navy" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Arial"
ForeColor="Black"></asp:label><asp:panel id="Panel1" style="Z-INDEX:
102; LEFT: 119px; POSITION: absolute; TOP: 216px" runat="server"
Width="550px" Height="136px" BackColor="Lavender"
BorderColor="DarkBlue" BorderStyle="Solid" BorderWidth="1px"
ForeColor="Black">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%"
align="center" border="0">
<TR>
<TD style="WIDTH: 169px; HEIGHT: 24px">
<asp:Label id="lblDept" runat="server"
Font-Bold="True">Department Name</asp:Label></TD>
<TD style="HEIGHT: 24px">
<asp:DropDownList id="lstDepartments" runat="server"
Font-Size="X-Small"></asp:DropDownList></TD>
</TR>
<TR>
<TD style="WIDTH: 169px">
<asp:Label id="lblPassword" runat="server"
Font-Bold="True">Password</asp:Label></TD>
<TD>
<asp:TextBox id="txtPassword" tabIndex="1" runat="server"
Width="181px" Font-Size="X-Small" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server" Font-Names="Arial" BackColor="Transparent"
Width="244px" Font-Size="Small" ControlToValidate="txtPassword"
ErrorMessage=" * Please enter a
password"></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD align="center" colSpan="2"><INPUT style="WIDTH: 90px; HEIGHT:
24px" tabIndex="2" type="submit" value="Login" runat="server"
onserverclick="cmdLogin_Click"></TD>
</TR>
</TABLE>
</asp:panel><asp:label id="lblDeptID" style="Z-INDEX: 103; LEFT:
125px; POSITION: absolute; TOP: 378px"
runat="server" Width="537px" Font-Names="Arial" ForeColor="Red"
Font-Size="Small"></asp:label><asp:panel id="Panel2" style="Z-INDEX:
100; LEFT: 28px; POSITION: absolute; TOP: 15px" runat="server"
Width="713px" Height="391px" BackColor="LightSteelBlue"
BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px"></asp:panel></form>
</body>
</HTML>

===========

Imports System.Web.Security
Public Class WebForm1
Inherits System.Web.UI.Page
Dim connString = ConfigurationSettings.AppSettings("connString")


#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lblInstructions As
System.Web.UI.WebControls.Label
Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
Protected WithEvents lblDept As System.Web.UI.WebControls.Label
Protected WithEvents txtPassword As
System.Web.UI.WebControls.TextBox
Protected WithEvents RequiredFieldValidator1 As
System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents lstDepartments As
System.Web.UI.WebControls.DropDownList
Protected WithEvents lblDeptID As System.Web.UI.WebControls.Label
Protected WithEvents Panel2 As System.Web.UI.WebControls.Panel

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here


If Not IsPostBack Then


'set label textfor login instructions
lblInstructions.Text = "<Div align='center'><H3>Welcome to
the Cityof Stamford OSHA Recording Application.</H3></DIV><p>"
lblInstructions.Text = lblInstructions.Text &
"<BlockQuote>In order to log into this application you must:<br>"
lblInstructions.Text = lblInstructions.Text & "<UL><LI>
Select your Department name from the dropdown list.<br>"
lblInstructions.Text = lblInstructions.Text & "<LI> Enter
your department's password in the box.<br>"
lblInstructions.Text = lblInstructions.Text & "<LI> Click
the Login Button<br></UL></BlockQuote>"


Dim myConnection As SqlClient.SqlConnection
Dim myCommand As SqlClient.SqlCommand

myConnection = New SqlClient.SqlConnection(connString)
myCommand = New SqlClient.SqlCommand("Select * from
Departments Order By DeptName", myConnection)
myConnection.Open()

lstDepartments.DataSource =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
lstDepartments.DataTextField = "DeptName"
lstDepartments.DataBind()

End If

'set focus to select box
Dim strBuilder As System.Text.StringBuilder = New
System.Text.StringBuilder
strBuilder.Append(" <script language='javascript'> ")
strBuilder.Append("
document.getElementById('lstDepartments').focus(); ")
strBuilder.Append(" </Script> ")

RegisterStartupScript("Focus", strBuilder.ToString)



End Sub

Public Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

'Stored Procedure - name: getDept
'Uses two input parameters

Dim oCN As SqlClient.SqlConnection
Dim oCmd As SqlClient.SqlCommand
'Response.Write(lstDepartments.SelectedValue & "<br>")
'Response.Write(txtPassword.Text & "<br>")

oCN = New SqlClient.SqlConnection(connString)
oCN.Open()

oCmd = New SqlClient.SqlCommand("getDept", oCN)
oCmd.CommandType = CommandType.StoredProcedure

oCmd.Parameters.Add(New SqlClient.SqlParameter("@deptName",
SqlDbType.VarChar, 50))
oCmd.Parameters("@deptName").Value =
lstDepartments.SelectedValue
oCmd.Parameters("@deptName").Direction =
ParameterDirection.Input

oCmd.Parameters.Add(New SqlClient.SqlParameter("@Password",
SqlDbType.VarChar, 50))
oCmd.Parameters("@Password").Value = txtPassword.Text
oCmd.Parameters("@Password").Direction =
ParameterDirection.Input

oCmd.Parameters.Add(New SqlClient.SqlParameter("@deptID",
SqlDbType.VarChar, 5))
oCmd.Parameters("@deptID").Direction = ParameterDirection.Output

oCmd.ExecuteNonQuery()

Dim vReturn As String
vReturn = oCmd.Parameters("@deptID").Value

If vReturn <> "-1" Then

FormsAuthentication.RedirectFromLoginPage(oCmd.Parameters("@deptID").Val
ue, False)
Else
lblDeptID.Text = "Your Password was not found. Please
re-enter your information."
End If

oCN.Close()

End Sub

End Class

====================

Michael
 
When did the problem happen, when you hit Enter key in the Text box or on
the submit button?

Luke

"Microsoft Security Announcement: Have you installed the patch for
Microsoft Security Bulletin MS03-026?? If not Microsoft strongly advises
you to review the information at the following link regarding Microsoft
Security Bulletin MS03-026
http://www.microsoft.com/security/security_bulletins/ms03-026.asp and/or to
visit Windows Update at http://windowsupdate.microsoft.com to install the
patch. Running the SCAN program from the Windows Update site will help to
insure you are current with all security patches, not just MS03-026."
 
Luke,

The problem occurs when the textbox has focus. I have observed that a
typical behavior for users to to type in their usernams / password and
hit the Enter key. This leaves the focus on the textbox while the submit
is enacted.

I have tried capturing keystrokes from within the textbox but have not
been sucessful in doing so.

Thanks,

Michael
 
For example:

<INPUT id="pwd" type="password">

Luke

"Microsoft Security Announcement: Have you installed the patch for
Microsoft Security Bulletin MS03-026?? If not Microsoft strongly advises
you to review the information at the following link regarding Microsoft
Security Bulletin MS03-026
http://www.microsoft.com/security/security_bulletins/ms03-026.asp and/or to
visit Windows Update at http://windowsupdate.microsoft.com to install the
patch. Running the SCAN program from the Windows Update site will help to
insure you are current with all security patches, not just MS03-026."
 
Back
Top