ASP.NET输入密钥

时间:2011-11-28 08:36:36

标签: asp.net validation

我有一个母版页,基于该母版页,我创建了一个页面,其中包含一个TextBox和两个验证控件,用于RequiredValidatorRegularExpressionValidator以及一个ValidationSummary

当按下TextBox中的回车键时,我预计两个验证器都会在validationSummary上显示它们的errorMessage,但这不是它发生的事情,只有当我按下页面上的按钮时才会有效。

根据此页面,我使用<asp:panel>DefaultButton属性包装了我的代码,但它没有解决问题。 http://www.codeproject.com/KB/aspnet/aspnet_Enter_key_problem.aspx

我想知道这里的问题是什么以及是否有解决方法?

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">

    **<asp:Panel runat="server" DefaultButton="EmailSend">**
    <table style="width: 100%">
        <tr>
            <asp:ValidationSummary ID="EmailValidation" runat="server" CssClass="failureNotification"
                ValidationGroup="EmailValidation" />
        </tr>
        <tr>
            <td style="width: 76px">
                Email:
            </td>
            <td class="style1">
                <asp:TextBox ID="EmailForget" runat="server" Width="244px"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="EmailRequiered" runat="server" ControlToValidate="EmailForget"
                    CssClass="failureNotification" ErrorMessage="RequiredFieldValidator" ValidationGroup="EmailValidation">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="EmailRegular" runat="server" ControlToValidate="EmailForget"
                    CssClass="failureNotification" ErrorMessage="email required"
                    ValidationGroup="EmailValidation">*</asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td style="width: 76px">
                &nbsp;
            </td>
            <td class="style1">
                <asp:Button ID="EmailSend" runat="server" Text="send" Width="56px" ClientIDMode="Static" />
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
   **<asp:Panel>**
</asp:Content>

3 个答案:

答案 0 :(得分:0)

如果您正在使用IE,那么根据这两页的答案:

ASP.NET enter key and form submit with no javascript

Enter button does not submit form (IE ONLY) ASP.NET

您只需要在表单中添加以下内容

<!-- Fix for IE bug submit on pressing "Enter") -->
<div style="display:none">
    <input type="text" name="hiddenText"/>
</div>

答案 1 :(得分:0)

我解决了这个问题:

protected void Page_Load(object sender, EventArgs e)
{
textboxusername.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13)       __doPostBack('" + LinkButton1.UniqueID + "','')");
    textboxpassword.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13)   __doPostBack('" + LinkButton1.UniqueID + "','')");
}

这是链接..

<asp:LinkButton ID="LinkButton1" runat="server" class="btn" OnClick="LinkButton1_Click">

答案 2 :(得分:0)

尝试添加此代码

<script type="text/javascript">

    if (document.addEventListener) {//if Firefox
        document.addEventListener("keypress", fireFoxHandler, true);
    } else {
    document.attachEvent("onkeyup", ieHandler);
    }

    function fireFoxHandler(evt) {            
        if (evt.keyCode == 13) {
            document.getElementById("EmailSend").click();
        }
    }

    function ieHandler(evt) {
        if (evt.keyCode == 13) {
            document.getElementById("EmailSend").click();
        }
    }

</script>

找到此解决方案here。 (我没有在MasterPage场景中检查这个,请检查......)