Enter key

I have a main page and based on this main page I created a page with a TextBox and two Validation controls for RequiredValidatorand RegularExpressionValidatorand one ValidationSummary.

when I hit the enter key in a TextBox, I expected both validators to map their errorMessage to validationSummary, but this is not what happens, it only works when you click a button on the page.

According to this page, I wrapped my code with attributes <asp:panel>and DefaultButton, but this did not solve the problem. http://www.codeproject.com/KB/aspnet/aspnet_Enter_key_problem.aspx

I want to know what the problem is, and is there a workaround?

<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>
0
source share
3 answers

Try adding this code.

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

. ( MasterPage, , ...)

0

IE, :

ASP.NET javascript

( IE) ASP.NET

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

I solved this problem as follows:

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 + "','')");
}

and this is the link ..

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

All Articles