Reputation: 2407
So basically I have a couple of textboxes which I would only like to accept numbers in them however I cannot get the JQuery Im using below to work with a textbox inside a ListView nor with another asp.net page.
$(document).ready(function () {
$("#<%= txtSearch.ClientID %>").keydown(function (event) {
// Allow: backspace, delete, tab, escape, and enter
if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
// Allow: Ctrl+A
(event.keyCode == 65 && event.ctrlKey === true) ||
// Allow: home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
else {
// Ensure that it is a number and stop the keypress
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
});
This code works with a normal textbox called txtSearch but when I insert txtQty insead of txtSearch it does not work :s
<asp:ListView ID="lvProducts" runat="server" OnItemCommand="lbProducts_ItemCommand" OnSelectedIndexChanged="lvProducts_SelectedIndexChanged">
<LayoutTemplate>
<div class="productoutercontainer">
<div id="itemPlaceHolder" runat="server"></div>
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="productinnercontainer">
<img class="pics" src="<%#Eval("ProductImage") %>" alt="<%#Eval("ProductName") %>" title="<%#Eval("ProductName") %>" />
<asp:Label ID="lblName" runat="server" Text='<% # Eval("ProductName") %>' Font-Bold="true" Font-Size="Medium"></asp:Label><br /><br />
Quantity: <asp:TextBox ID="txtQty" runat="server" columns="3"/>
<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CssClass="button2" CommandArgument='<%# Eval("ProductId")%>' OnClick="btnAddToCart_Click" /> <br /><br /><br />
</div>
</ItemTemplate>
</asp:ListView>
Upvotes: 1
Views: 881
Reputation: 759
Each textbox in the listview will have a different client id. Run the code and look at the generated client code by doing view source on your browser. Check the ids of txtQty textboxes.
Give txtQty a CssClass. Just give a name no need for css coding. Let say:
<asp:TextBox ID="txtQty" CssClass="qty" runat="server" columns="3"/>
Change this:
$("#<%= txtSearch.ClientID %>")
to this:
$(".qty")
Now it has to work...
Upvotes: 1