I'm trying to cancel a submit when I click enter in an ASP:TextBox
element which is performing asyncpostback
when text is changed and make it click on other button instead. I use the same Javascript function for all of my textboxes, but the others are defined as regular input
elements and do not have any asyncpostback
method. It works with each of the textboxes but not with the asp:TextBox
one. I'm not sure why, but I'd be glad if you knew how to fix this issue. When I click enter in the textbox, it updates asynchronously as it should, but immediately refreshing the page.
This is my code:
function cancel_enter(elem) {
if (window.event.keyCode == 13) {
document.getElementById("btnReg").focus();
document.getElementById("btnReg").click();
if (elem != null)
elem.focus();
return false;
}
else
return true;
return true;
}
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="user_info_all" runat="server">
<table dir="rtl" style="text-align:left;" id="table_user" runat="server">
<tr>
<td>שם משתמש:</td>
<td><asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"><ContentTemplate>
<asp:TextBox TextMode="SingleLine" ID="reg_user" runat="server" MaxLength="50" OnTextChanged="reg_user_TextChanged" AutoPostBack="true"></asp:TextBox>
<div class="input-validation" id="reg_user_v" runat="server"></div></ContentTemplate>
<Triggers><asp:AsyncPostBackTrigger ControlID="btnReg" /></Triggers></asp:UpdatePanel>
</td></tr>
<tr>
<td>סיסמא:</td>
<td><div class="tooltip" title="הסיסמא חייבת להכיל 8 תווים לפחות">
<input class="tooltip" title="" type="Password" ID="reg_pass" runat="server" maxlength="50" onkeypress="return cancel_enter(this);" onkeyup="ValidateEmailPass(this);" /></div>
<div class="input-validation" id="reg_pass_v"></div></td></tr>
<tr>
<td>אימייל:</td>
<td>
<input type="text" id="reg_email" runat="server" maxlength="100" onkeypress="return cancel_enter(this);" onkeyup="ValidateEmailPass(this);" />
<div class="input-validation" id="reg_email_v"></div></td></tr>
<tr>
<td>גיל:</td>
<td>
<input type="text" id="reg_age" maxlength="2" runat="server" onkeypress="return cancel_enter(this);" onkeyup="ValidateEmailPass(this);" />
<div class="input-validation" id="reg_age_v"></div></td></tr>
<tr>
<td colspan='2' style="text-align:center"><asp:UpdatePanel ID="RegisterBtn" runat="server" UpdateMode="Conditional"><ContentTemplate>
<asp:Button runat="server" ID="btnReg" OnClientClick="return HideForm(this);" OnClick="btnReg_Click" Text="הירשם" UseSubmitBehavior="true" /></ContentTemplate></asp:UpdatePanel></td></tr>
</table>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnReg" />
</Triggers>
</asp:UpdatePanel>
Why is it still refreshing the page after I click enter?