开发者

How to call a Button press Event even with the press of the Enter key on keyboard

the following is the Markup of my page based on a master page. its a log in page with a default login control.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2 style="text-align:center; font-size:1.2em; color:White;">
    Log In
</h2>
<p style="text-align:left; font-size:1.2em; color:White;margin-left: 90px;margin-right: 90px;">
    Please enter your username and password.<br />
    If you don't have an account, please contact the site administrator to set up 
    your account.
</p>
<div style="margin-left:0px;">


<asp:Login ID="LoginUser" runat="server" EnableViewState="False" >
    <LayoutTemplate>
        <span class="failureNotification">
            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
        </span>
        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
             ValidationGroup="LoginUserValidationGroup"/>
        <div class="accountInfo">
            <fieldset class="login">
                <legend style="text-align:left; font-size:1.2em; color:White;">Account Information</legend>
                <p style="text-align:left; font-size:1.2em; color:White;">
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User ID:</asp:Label>
                    <asp:TextBox ID="UserName" runat="server" CssClass="textEntry" Width="176px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                         CssClass="failureNotification" ErrorMessage="User ID is required." ToolTip="User ID field is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p style="text-align:left; font-size:1.2em; color:White;">
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                    <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" 
                        TextMode="Password" Width="176px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                         CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p style="text-align:left; font-size:1.2em; color:White;">
                    <asp:CheckBox ID="RememberMe" runat="server"/>
                    <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
                </p>
            </fieldset>

            <p class="submitButton">
                &nbsp;<asp:Panel ID="LoginPanel" runat="server"  DefaultButton="LoginButton">
                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                    ValidationGroup="LoginUserValidationGroup" onclick="LoginButton_Click"/>
            </asp:Panel>
                <p>
                </p>
            </p>

        </div>
    </LayoutTemplate>
</asp:Login>


</div>

I wish to know how to let the login buttons click even fire when the users press enter on the keyboard.

Do i have to do the codeing in the .cs page or it can be done in the markup? Please help

Page update with adding javascript in the master page body tag

<body onload="updateClock(); setInterval('updateClock()', 1000 )" onkeypress="checkenter();">
<script type="text/javascript">
    function checkenter() 
    {
        if (event.keyCode == 13) 
        { //if they pressed enter key then..
            document.formname.LoginButton.focus();

        }
    }
</script>
<form id="Form1" runat="server">
<div class="page">
    <div class="header">
        <div class="title">

LogIn click event action

protected void LoginButton_Click(object sender, EventArgs e)
    {


        //assign textbox items to string objects
        userIDStr = LoginUser.UserName.ToString();
        pwrdStr = LoginUser.Password.ToString();

        //SQL connection string

        string strConn;
        strConn = WebConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString1"].ConnectionString;

        SqlConnection Conn = new SqlConnection(strConn);



        //SqlDataSource CSMDataSource = new SqlDataSource();
        // CSMDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ToString();


        //SQL select statement for comparison

        string sqlUserData;
        sqlUserData = "SELECT UserID, UserPassword, UserName, UserType FROM Users";
        sqlUserData += " WHERE (UserID ='" + userIDStr + "')";
        sqlUserData += " AND (UserPassword ='" + pwrdStr + "')";

        SqlCommand com = new SqlCommand(sqlUserData, Conn);
        SqlDataReader rdr;
        string usrdesc;
        string Uname;


        try
        {

            //string CurrentData;
            //CurrentData = (string)com.ExecuteScalar();
            Conn.Open();
            rdr = com.ExecuteReader();
            rdr.Read();
            usrdesc = (string)rdr["UserType"];
            Uname = (string)rdr["UserName"];


            LoginUser.UserName = Uname.ToString();

            rdr.Close();
            Session["userID"] = userIDStr;
            Session["userType"] = usrdesc;


            if (usrdesc.ToLower() == "administrator")
            {
                FormsAuthentication.RedirectFromLoginPage(Uname, false);
                Response.Redirect("~/CaseAdminPage.aspx"/*?adminID=" + userIDStr*/, false);

            }
            else if (usrdesc.ToLower() == "manager")
            {
                FormsAuthentication.RedirectFromLoginPage(Uname, false);
                Response.Redirect("~/ManagerPage.aspx"/*?mgrID=" + userIDStr*/开发者_Python百科, false);
            }
            else if (usrdesc.ToLower() == "investigator")
            {
                FormsAuthentication.RedirectFromLoginPage(Uname, false);
                Response.Redirect("~/Investigator.aspx"/*?investigatorID=" + userIDStr*/, false);
            }
            else
            {
                Response.Redirect("~/Default.aspx", false);
            }


        }
        catch (Exception ex)
        {
            string script = "<script>alert('" + ex.Message + "');</script>";
        }
        finally
        {
            Conn.Close();
        }


    }


Have you come across this article?

The defaultbutton attribute may be just what your looking for.

http://geekswithblogs.net/ranganh/archive/2006/04/12/74951.aspx

Good luck.


One way I think you can get it to work is by using JavaScript

<body onkeypress="checkenter();" >


<script language="javascript">
function checkenter() {
  if (event.keyCode == 13) { //if they pressed enter key then..
    document.formname.LoginButton.focus();
  }
}
</script>


Change the div surrounding your login control to an <asp:Panel> control. Then set the DefaultButton property to LoginButton:

<asp:Panel ID="LoginPanel" runat="server" style="margin-left:0px;" DefaultButton="LoginButton">

    ....

</asp:Panel>

EDIT: code snippet update

    <!-- Notice I've changed the <div class="accountInfo"> with the
         asp panel to encompass the the textbox controls and the login button -->
    <asp:Panel CssClass="accountInfo" ID="LoginPanel" runat="server" DefaultButton="LoginButton">
        <fieldset class="login">
            <legend style="text-align:left; font-size:1.2em; color:White;">Account Information</legend>
            <p style="text-align:left; font-size:1.2em; color:White;">
                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User ID:</asp:Label>
                <asp:TextBox ID="UserName" runat="server" CssClass="textEntry" Width="176px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                     CssClass="failureNotification" ErrorMessage="User ID is required." ToolTip="User ID field is required." 
                     ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
            </p>
            <p style="text-align:left; font-size:1.2em; color:White;">
                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" 
                    TextMode="Password" Width="176px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                     CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                     ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
            </p>
            <p style="text-align:left; font-size:1.2em; color:White;">
                <asp:CheckBox ID="RememberMe" runat="server"/>
                <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
            </p>
        </fieldset>

        <p class="submitButton">
            &nbsp;
            <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                ValidationGroup="LoginUserValidationGroup" onclick="LoginButton_Click"/>

            <p>
            </p>
        </p>

    </asp:Panel>


If there's only one submit button you can put an onsubmit handler in the form tag. I don't really know how to do that with your ASP code, but hopefully you do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜