I m working on a web application that contains a text box. The value of text box is filled from Date picker and its property is read only by default. till now everything is working fine. But problem occurs when I press backspace button. Rather than clearing the text box it is redirecting me back on the previous page.
i handled this situation by a javascript function by referring some previous answers on this site.
function allowBackSpace(val) {
var keyCodeEntered = (event.which) ?
event.which :
(window.event.keyCode) ?
window.event.keyCode :
-1;
if (keyCodeEntered == 8) {
$(this).val("");
return true;
}
return false;
}
UPDATE : this function is called from
<asp:TextBox ID="txtDate"
Style="margin-left: 5px; margin-right: 5px"
runat="server"
Width="88px"
onkeypress="return allowBackSpace(this);">
</asp:TextBox>
I m calling this function on key press event of the textbox. The preoblem is that it is allowing only to delete one character at a time.
I wish to clear the whole textbox if user presses backspace button. Stuck here as how to achieve this.
Thanks in advance
Akhil
PS:Any way to achieve this using C# code will also be very helpful.
You are returning true from js function, which also performs its keypress event. So, try with returning false.
You can try the following code: