Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Trapping "Enter" keypress event for an HTML form


Message #1 by "Blackwell, Cotten - Accent" <cotten.blackwell@a...> on Fri, 28 Jun 2002 09:56:44 -0700
Check this out: http://www.dotnet247.com/247reference/msgs/10/54331.aspx

> Folks:

Thanks again to all those who submitted the helpful information regarding 
my
last post.

This time, I have one oddball page in my ASPX project where for some reason
I can't figure out, the "Enter" key is not causing the submit button to
trigger.  As you can see from the code, below, this is a super-simple form,
just a text box and a submit button.

Digging through the .NET docs, this would be too easy to fix if this were a
WinForms app -- I could just set the form.AcceptButton property.  However,
no such property for the HtmlForm class...

Also haven't managed to dig up anything on how to capture the keypress 
event
coming from the textbox and delegate it to the submit button handler that's
already written.  I'd prefer to learn how to deal with this in the
code-behind file rather than write a JavaScript fix.

My ASPX page and code-behind file are attached below.  Any ideas?

Thanks in advance,

Cotten Blackwell

cotten.blackwell@a...
Voice:	(360) 882-4002, ext. 208
Fax:	(360) 882-4006
 <<...OLE_Obj...>> 

-------------------


<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="ForgotPassword.aspx.vb" Inherits="SalemElectric.ForgotPassword"
%>
<%@ Register tagprefix="SE" TagName="header" Src="SE_Header.ascx" %>
<%@ Register tagprefix="SE" TagName="footer" Src="SE_Footer.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>Forgot Password</title>
		<meta content="Microsoft Visual Studio.NET 7.0"
name="GENERATOR">
		<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta
content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
		<!--This file created 4:52 PM  11/30/1999 by Claris Home
Page version 3.0-->
		<META content="Microsoft FrontPage 4.0" name="GENERATOR">
		<X-CLARIS-WINDOW TOP="25" BOTTOM="416" LEFT="13"
RIGHT="543">
			<X-CLARIS-TAGVIEW MODE="minimal">
				<META http-equiv="content-type"
content="text/html;charset=iso-8859-1">
				<META content="Salem Electric is a publicly
owned electric utitliy serving West Salem and Keizer Oregon."
name="DESCRIPTION">
				<META content="Salem, Oregon, electric,
utility," name="KEYWORDS">
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<!-- Page header with navbars begins here... -->
		<se:header runat="server" id="SE_Header" />
		<!-- Page content begins here.... -->
		<td vAlign="top" colSpan="2">
			<!--Begin Main Content Area -->
			<form id="frmForgotPwd" method="post"
runat="server">
				<!-- <TABLE id="tblLogin" cellSpacing="1"
cellPadding="0" width="600" border="1"> --><FONT face="Arial"><STRONG><EM>
							<TABLE 
id="tblLogin"
cellSpacing="1" cellPadding="1" border="0">
								<TBODY>
								
	<TR>
	
<TD colSpan="2">
	
<asp:Label id="lblForgotPassword" runat="server" Font-Bold="True">Forgot My
Password</asp:Label>
						</EM></STRONG>
		</td>
		<TD colspan="2">
			If you are unable to use this page, please call 503
362-3601.</TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 144px" align="right"></TD>
			<TD style="WIDTH: 150px"><asp:label
id="lblBadEmailAddr" runat="server" Font-Size="Smaller" Width="336px"
Visible="False" ForeColor="Red">
	
This is not 
	
a valid email address. Please 
	
try again or contact customer service at 503 362-3601.
	
</asp:label></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 144px" align="right"></TD>
			<TD style="WIDTH: 
150px"><asp:requiredfieldvalidator
id="rfvEmailAddr" runat="server" Font-Size="Smaller" Width="328px"
ControlToValidate="txtEmailAddress" ErrorMessage="Please enter an address
currently on file with Salem Electric."
Display="Dynamic"></asp:requiredfieldvalidator></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 144px" align="right"></TD>
			<TD style="WIDTH: 150px"></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 144px" align="right"></TD>
			<TD style="WIDTH: 150px">
				<asp:label id="Label1" runat="server"
ForeColor="Red" Font-Size="Smaller" Font-Italic="True">* = required
field</asp:label></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 144px" align="right"><asp:label
id="lblUserNameReq" runat="server" ForeColor="Red">*</asp:label><asp:label
id="lblEmailAddr" runat="server">Email Address</asp:label>
			<TD>
				<asp:TextBox id="txtEmailAddress"
runat="server" 
Width="320px">YourEmailAddress@M...</asp:TextBox></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 158px"></TD>
			<TD style="WIDTH: 150px"></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 158px"></TD>
			<TD style="WIDTH: 150px">
				<asp:button id="btnGetPwd" runat="server"
Text="Get Password"></asp:button></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 158px"></TD>
			<TD style="WIDTH: 150px"></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 158px"><asp:linkbutton
id="lnkRegister" runat="server" Width="88px">Register</asp:linkbutton></TD>
			<TD style="WIDTH: 150px">
				<asp:label id="lblCurrentCustomer"
runat="server" Font-Size="Smaller" Width="336px" ForeColor="Red"
Height="8px">
	
(Current and previous Salem Electric customers only)</asp:label></TD>
			<TD></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 325px" colSpan="3"></FONT></TD>
		</TR>
		<TR>
			<TD style="WIDTH: 26px"></TD>
			<TD style="WIDTH: 158px"></TD>
			<TD style="WIDTH: 150px"></TD>
			<TD></TD>
		</TR>
		</TBODY></TABLE></FORM></TD>
		<DIV></DIV>
		<!-- end of main content area... -->
		<!-- Now, wrap up the HTML loose ends with the page
footer... -->
		<se:footer runat="server" id="SE_Footer"
/></EM></STRONG></FONT>
	</body>
</HTML>

--------

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Imports System.Web.Mail
Imports System.Web.Mail.SmtpMail
Imports System.Web.UI.HtmlControls

Public Class ForgotPassword
    Inherits System.Web.UI.Page
    Protected WithEvents lblForgotPassword As
System.Web.UI.WebControls.Label
    Protected WithEvents lblBadEmailAddr As System.Web.UI.WebControls.Label
    Protected WithEvents rfvEmailAddr As
System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents lblUserNameReq As System.Web.UI.WebControls.Label
    Protected WithEvents lblEmailAddr As System.Web.UI.WebControls.Label
    Protected WithEvents txtEmailAddr As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnGetPwd As System.Web.UI.WebControls.Button
    Protected WithEvents lnkRegister As 
System.Web.UI.WebControls.LinkButton
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents lblCurrentCustomer As
System.Web.UI.WebControls.Label
    Protected WithEvents txtEmailAddress As
System.Web.UI.WebControls.TextBox
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents frmForgotPwd As 
System.Web.UI.HtmlControls.HtmlForm

#Region " Web Form Designer Generated Code "

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

    End Sub

    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
        'Guess AcceptButton is only a property of a WinForms form :(
        'Me.frmForgotPwd.AcceptButton = Me.btnGetPwd

    End Sub

    Private Function SendMail(ByVal strSendTo As String, _
                                ByVal strSubject As String, _
                                ByVal strBody As String) As Boolean
        Dim myMsg As New MailMessage()

        myMsg.To = strSendTo

        'Be sure to change this to the official SE email address (cust.
service?)
        myMsg.From = "se@s..."

        myMsg.Subject = strSubject
        myMsg.BodyFormat = MailFormat.Text
        myMsg.Body = strBody

        SmtpMail.SmtpServer = "accent-inc.com"
        SmtpMail.Send(myMsg)
        SendMail = True

    End Function

    Private Function touchDB(ByVal strSQL As String) As DataSet
        Dim SEConn As OleDbConnection = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Application("strWebRoot") &
"SalemElectric\SEAccounts\SEClientData.mdb;")

        ' Use the DataAdapter to fill and update the DataSet.
        Dim objDA As OleDbDataAdapter = New OleDbDataAdapter(strSQL, 
SEConn)

        SEConn.Open()

        ' Fill the DataSet.
        Dim objDS As DataSet = New DataSet()
        objDA.Fill(objDS)

        touchDB = objDS

        If SEConn.State = ConnectionState.Open Then
            SEConn.Close()
            SEConn.Dispose()
        End If

    End Function

    Private Sub btnGetPwd_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles btnGetPwd.Click
        ' Use the DataAdapter to fill and update the DataSet.
        Dim strSQL As String
        strSQL = "SELECT * FROM [Patron Table]" _
                    & " WHERE [Patron Table].EmailAddr='" &
txtEmailAddress.Text.Trim() & "'"
        ' Fill the DataSet.
        Dim objDS As DataSet = New DataSet()
        objDS = touchDB(strSQL)

        If objDS.Tables(0).Rows.Count > 0 Then
            'set a session variable for this user
            Session("EmailAddr") = txtEmailAddress.Text
            'Session("EmailAddr") = "doe@d..."
            'Session("EmailAddr") =
objDS.Tables(0).Columns("EmailAddr").ToString

            'Me.lblForgotPassword.Text = "All good! Session.EmailAddress is
now " & _
            '    Session("EmailAddr")
            Me.lblBadEmailAddr.Visible = False

            'mail it!
            Dim strSendTo As String
            'strSendTo = txtEmailAddress.Text
            strSendTo = "cotten.blackwell@a..."
            Dim strSubject As String
            strSubject = "Your SalemElectric.org info"
            Dim strBody As String
            strBody = "Dear Salem Electric Patron:" _
                        & vbCrLf & vbCrLf _
                        & "Here are your SalemElectric.org username and
password:" _
                        & vbCrLf & vbCrLf _
                        & vbTab & "Username:" & vbTab _
                                &
Nz(objDS.Tables(0).Rows(0).Item("UserName"), "Unknown") _
                        & vbCrLf _
                        & vbTab & "Password:" & vbTab _
                                &
Nz(objDS.Tables(0).Rows(0).Item("Password"), "Unknown") _
                        & vbCrLf & vbCrLf _
                        & "If you have any trouble logging in to the 
website
with the " _
                        & "above information, please call customer service
at 503 362-3601."

            Dim blnSentOK As Boolean
            blnSentOK = SendMail(strSendTo, strSubject, strBody)

            If blnSentOK Then
                Response.Redirect("PasswordMailed.aspx")
            End If
        Else
            Me.lblBadEmailAddr.Visible = True

        End If

    End Sub

    Public Function Nz(ByVal varValue As Object, ByVal
varReturnIfValueIsNull As String) As String
        If IsDBNull(varValue) Then
            Nz = varReturnIfValueIsNull
        Else
            Nz = varValue.ToString()
        End If
    End Function

End Class



  Return to Index