即使按下键盘上的Enter键,如何调用按钮按下事件

时间:2011-01-21 15:43:07

标签: c# javascript asp.net

以下是基于母版页的我的页面标记。它是一个带有默认登录控件的登录页面。

<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>

我希望知道当用户按下键盘输入时,如何让登录按钮点击甚至点火。

我是否必须在.cs页面中进行编码,或者可以在标记中完成? 请帮忙

通过在母版页主体标记中添加javascript进行页面更新

<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">

登录点击事件操作

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*/, 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();
        }


    }

4 个答案:

答案 0 :(得分:2)

您是否看过这篇文章?

defaultbutton属性可能正是您所寻找的。

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

祝你好运。

答案 1 :(得分:2)

我认为你可以通过使用JavaScript来实现它的一种方式

<body onkeypress="checkenter();" >


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

答案 2 :(得分:1)

将登录控件周围的div更改为<asp:Panel>控件。然后将DefaultButton属性设置为LoginButton:

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

    ....

</asp:Panel>

编辑:代码段更新

    <!-- 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>

答案 3 :(得分:0)

如果只有一个提交按钮,则可以在表单标记中放置onsubmit处理程序。我真的不知道如何使用你的ASP代码,但希望你这样做。