【问题标题】:ASP.NET Enter keyASP.NET 回车键
【发布时间】:2011-11-28 08:36:35
【问题描述】:

我有一个母版页,并基于该母版页创建了一个页面,其中包含一个 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>

【问题讨论】:

  • 您是否尝试过将验证组也添加到 asp:button 中?
  • 您在 ValidationSummary 控件周围漏掉了一个 标签,请先添加它!
  • @Jacques 是的,我已将 asp:button 添加到验证组
  • @Nalaka526 我的真实代码中有 td 标签。谢谢。

标签: asp.net validation


【解决方案1】:

尝试添加此代码

<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 场景中检查这个,请检查...)

【讨论】:

  • @Nalaka525 非常感谢它即使在 MasterPage 场景中也能正常工作。
  • 我发现了这个forums.asp.net/t/1100563.aspx/1,它解释了为什么 ValidationSummary 和 Enter 键不能一起工作。
【解决方案2】:

如果你使用的是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>

【讨论】:

  • 感谢您的快速回复。但是 chrome 和 Firefox 也不存在同样的问题。
【解决方案3】:

我这样解决了这个问题:

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

【讨论】:

  • 感谢您的回复,我尝试了您的脚本,但没有成功。我还在页面 loadPage 上添加了这两行。 Form.DefaultButton = EmailSend.UniqueID; Page.Form.DefaultFocus = EmailForget.ClientID;它也没有用。
猜你喜欢
相关资源
最近更新 更多
热门标签