I have a login page that when I hit the enter key nothing happens. I'm using a Masterpage so I cannot use a sub <form> tag in my login page. How can I make the login page adhere to the enter key?
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.SqlClient
Partial Public Class Login
Inherits System.Web.UI.Page
Private con As New SqlConnection()
Private adp As SqlDataAdapter
Private ds As New DataSet()
Protected Sub LoginBtnClick(sender As Object, e As System.EventArgs) Handles LoginButton.Click
Dim con As New SqlConnection("Data Source=MyServer;Initial Catalog=MyDB;User ID=MyID;Integrated Security=SSPI")
con.Open()
' here with the help of this sql query i am matching the username and password of //the user.
Dim cmd As New SqlCommand()
cmd.CommandText = "select username,password, role from Users where username=@username and password=@password"
cmd.Connection = con
' here i am passing the parameter of username and password
cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = txtusername.Text
cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = txtpassword.Text
Dim dr As SqlDataReader = cmd.ExecuteReader()
' here i am using hasrows to check the correct user. If the username and the passwor //of the user will be mathed with the database then it will be go to the checkuser.aspx //page otherwise it prints the message wrongusername or password
If dr.HasRows Then
dr.Read()
' here i am creating a formauthentication ticket that will be use in the whole //application.
'This is the main part of the formauthentication security, inside dr[2] //there is a role of the user
Dim tkt As New FormsAuthenticationTicket(1, txtusername.Text, DateTime.Now, DateTime.Now.AddHours(3), False, dr(2).ToString(), _
FormsAuthentication.FormsCookiePath)
' here i am enctypt the ticket. With the encryption of this ticket it will encrypt the //username
Dim st As [String] = FormsAuthentication.Encrypt(tkt)
' here i am creat a cookie that will we used inside the whole application
Dim ck As New HttpCookie(FormsAuthentication.FormsCookieName, st)
Response.Cookies.Add(ck)
' Validate the user against the Membership framework user store
'If Membership.ValidateUser(txtusername.Text, txtpassword.Text) Then
' Log the user into the site
'FormsAuthentication.RedirectFromLoginPage(txtusername.Text, RememberMe.Checked)
Response.Redirect("checkuser.aspx")
Else
LabelFailedAuthentication.Visible = True
End If
Dim ctl As Control = LoginButton.FindControl("LoginButton")
If ctl IsNot Nothing AndAlso TypeOf ctl Is IButtonControl Then
LoginButton.Attributes.Add("onkeypress", String.Format("javascript:return LoginButton(event, '{0}')", ctl.ClientID))
End If
End Sub
End Class
Dim ctl As Control = LoginButton.FindControl("LoginButton")
If ctl IsNot Nothing AndAlso TypeOf ctl Is IButtonControl Then
LoginButton.Attributes.Add("onkeypress", String.Format("javascript:return LoginButton(event, '{0}')", ctl.ClientID))
End If
I have login page with this code (Enter key and click both working)
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
If Page.IsValid Then
Dim UserTable As DataTable = CType(SqlDataCekUser.Select(New DataSourceSelectArguments), DataView).Table
If UserTable.Rows.Count >= 1 Then
FormsAuthentication.RedirectFromLoginPage(UserID.Text, CheckBox1.Checked)
Else
Msg.Text = "try again!"
End If
End If
End Sub
deongee
Member
21 Points
128 Posts
Enter Key not working Login page using MasterPage
Jul 15, 2012 08:25 PM|LINK
I have a login page that when I hit the enter key nothing happens. I'm using a Masterpage so I cannot use a sub <form> tag in my login page. How can I make the login page adhere to the enter key?
login.aspx
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" Title="My CMS: Please Login" %> <asp:Content ID="MainContent" ContentPlaceHolderID="MainContentPlaceHolder" runat="Server"> <div id="page-wraper"> <div id="in-page-wraper"> <div id="rt-mainbody-home"> <div class="component-content"> <div class="rt-leading-articles"> <div class="rt-article "> <div class="rt-article-bg"> <div class="rt-headline"> <div class="rt-headline"> <h1 class="rt-article-title">My CMS: Please Login</h1> </div> <div> </div> <div> <img src="../images/article-separation.jpg" alt="" width="100%" height="6px" style="margin-bottom: 2px;" /></div> </div> <!--Start All Feeds--> <asp:Label ID="Login1" runat="server" Text="Label" ForeColor="White"></asp:Label> <p>Username: <asp:TextBox ID="txtusername" runat="server" Width="150px" CssClass="form-td"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="User Name is Required" ControlToValidate="txtusername" ValidationGroup="a" CssClass="error" Font-Bold="True"></asp:RequiredFieldValidator></p> <p>Password: <asp:TextBox ID="txtpassword" runat="server" Width="150px" CssClass="form-td" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Password is Required" ControlToValidate="txtpassword" ValidationGroup="a" CssClass="error" Font-Bold="True"></asp:RequiredFieldValidator></p> <asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="../feeds/images/update.png" CausesValidation="false" CommandName="Update" ToolTip="Update" ImageAlign="AbsBottom" /> <asp:Button ID="LoginButton" runat="server" OnClick="LoginBtnClick" ValidationGroup="a" Text="Submit" CssClass="form-td" /><br /> <br /> <asp:Label ID="txtlbl" runat="server" Font-Bold="True" ForeColor="Red" Text="Authentication Request Failed" Visible="False"></asp:Label> </p> <div><img src="../images/article-separation.jpg" alt="" width="100%" height="6px" style="margin-bottom: 2px;" /></div> <p> <asp:Label ID="LabelFailedAuthentication" runat="server" Font-Bold="True" ForeColor="Red" Text="UserName or Password Combination Failed." Visible="False"></asp:Label></p> </div> </div> </div> <div> </div> </div> </div> </div> <!--End Silde Show--> <!-- End Silde Show--> <div id="rt-mainbody-sidenav"> <div class="component-content-sidenav"> <div class="homeleftnav"> <ul> <li><a id="i1" href="http://www.me.com/" target="blank">My Public Site</a></li> <li><a id="i2" href="http://www..me.com/" target="blank">Audits</a></li> <li><a id="i3" href="http://www..me.com/" target="blank">Quarterly Reports</a></li> <li><a id="i4" href="http://www..me.com" target="blank">Pressroom</a></li> <li><a id="i5" href="http://www..me.com" target="blank">News Feeds</a></li> </ul> </div> </div> </div> </div> </asp:Content>Login.aspx.vb
Imports System.Collections Imports System.Configuration Imports System.Data Imports System.Linq Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.HtmlControls Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Xml.Linq Imports System.Data.SqlClient Partial Public Class Login Inherits System.Web.UI.Page Private con As New SqlConnection() Private adp As SqlDataAdapter Private ds As New DataSet() Protected Sub LoginBtnClick(sender As Object, e As System.EventArgs) Handles LoginButton.Click Dim con As New SqlConnection("Data Source=MyServer;Initial Catalog=MyDB;User ID=MyID;Integrated Security=SSPI") con.Open() ' here with the help of this sql query i am matching the username and password of //the user. Dim cmd As New SqlCommand() cmd.CommandText = "select username,password, role from Users where username=@username and password=@password" cmd.Connection = con ' here i am passing the parameter of username and password cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = txtusername.Text cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = txtpassword.Text Dim dr As SqlDataReader = cmd.ExecuteReader() ' here i am using hasrows to check the correct user. If the username and the passwor //of the user will be mathed with the database then it will be go to the checkuser.aspx //page otherwise it prints the message wrongusername or password If dr.HasRows Then dr.Read() ' here i am creating a formauthentication ticket that will be use in the whole //application. 'This is the main part of the formauthentication security, inside dr[2] //there is a role of the user Dim tkt As New FormsAuthenticationTicket(1, txtusername.Text, DateTime.Now, DateTime.Now.AddHours(3), False, dr(2).ToString(), _ FormsAuthentication.FormsCookiePath) ' here i am enctypt the ticket. With the encryption of this ticket it will encrypt the //username Dim st As [String] = FormsAuthentication.Encrypt(tkt) ' here i am creat a cookie that will we used inside the whole application Dim ck As New HttpCookie(FormsAuthentication.FormsCookieName, st) Response.Cookies.Add(ck) ' Validate the user against the Membership framework user store 'If Membership.ValidateUser(txtusername.Text, txtpassword.Text) Then ' Log the user into the site 'FormsAuthentication.RedirectFromLoginPage(txtusername.Text, RememberMe.Checked) Response.Redirect("checkuser.aspx") Else LabelFailedAuthentication.Visible = True End If Dim ctl As Control = LoginButton.FindControl("LoginButton") If ctl IsNot Nothing AndAlso TypeOf ctl Is IButtonControl Then LoginButton.Attributes.Add("onkeypress", String.Format("javascript:return LoginButton(event, '{0}')", ctl.ClientID)) End If End Sub End Classoned_gk
All-Star
31055 Points
6356 Posts
Re: Enter Key not working Login page using MasterPage
Jul 16, 2012 02:05 AM|LINK
For what code below? remove it
Dim ctl As Control = LoginButton.FindControl("LoginButton")
If ctl IsNot Nothing AndAlso TypeOf ctl Is IButtonControl Then
LoginButton.Attributes.Add("onkeypress", String.Format("javascript:return LoginButton(event, '{0}')", ctl.ClientID))
End If
I have login page with this code (Enter key and click both working)
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
If Page.IsValid Then
Dim UserTable As DataTable = CType(SqlDataCekUser.Select(New DataSourceSelectArguments), DataView).Table
If UserTable.Rows.Count >= 1 Then
FormsAuthentication.RedirectFromLoginPage(UserID.Text, CheckBox1.Checked)
Else
Msg.Text = "try again!"
End If
End If
End Sub
deongee
Member
21 Points
128 Posts
Re: Enter Key not working Login page using MasterPage
Jul 16, 2012 03:44 PM|LINK
<script language="javascript" type="text/javascript"> document.onkeypress = keyhandler; function keyhander(e) { key = window.event.keyCode; if (key == 13) { var obj = document.getElementById('<%=LoginButton.ClientID%>'); obj.focus(); obj.focus(); } } </script>This works perfect. Thanks for the response.
Catherine Sh...
All-Star
23373 Points
2490 Posts
Microsoft
Re: Enter Key not working Login page using MasterPage
Jul 19, 2012 07:32 AM|LINK
Hi,
Very glad to hear that you have resolved your issue and thank you for sharing your solution.
Best wishes,
Feedback to us
Develop and promote your apps in Windows Store