Friday, October 8, 2010

HANDLING KEYPRESS EVENT OF TEXTBOX INSIDE GRIDVIEW

I have a GridView, in which my first column is Template Field which contains textbox.

<asp:gridview id="grdTest" runat="server" autogeneratecolumns="false" datakeynames="TestID">
<columns>
<asp:templatefield HeaderText="Amount">
<itemtemplate>
<asp:TextBox ID="txtAmount" runat="server" Text='' />
</itemtemplate>
<asp:boundfield datafield="Name" headertext="Name" />
</asp:TemplateField>
</columns>
</asp:GridView>

Now, I want that user can only enter integer value in txtAmount textbox. I want to handle this client side only. So, I used jquery and handled KeyPress event of textbox. By doing so, user can only enter digits. If user will press any non-numeric key it will not get entered.

<script type="text/javascript">
$(document).ready(function () {
$("#<%=grdTest.ClientID %> INPUT[id$='txtAmount']").keypress(
function IsNumericHandler(e) {
var unicode = e.charCode ? e.charCode : e.keyCode

//if the key isn't the backspace key (which we should allow)
if (unicode != 8) {
//if not a number
if (unicode <> 57)
//disable key press
return false;
}
});
});
</script>

No comments:

Post a Comment