Hey there! I'm currently diving into the world of JavaScript and C#. Feel free to correct me if you see any mistakes along the way.
Check out my gridview
code snippet below:
<asp:GridView ID="GridView1" CssClass="table table-hover table-bordered" runat="server" AutoGenerateColumns="False" ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="AvlQty" HeaderText="Available" ItemStyle-Width="35">
<ItemStyle Width="35px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Qty" ItemStyle-Width="70">
<ItemTemplate>
<asp:TextBox ID="TextBoxQty" onkeyup="Calculate(this)" CssClass="txtQty" runat="server" Text='<%# Eval("SelQty") %>' MaxLength="5" Width="45"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxQty" Display="Dynamic" ForeColor="Red" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
<asp:regularexpressionvalidator ID="revAvailablePeriod" runat="server" ErrorMessage="Must be greater than 0" ForeColor="Red" controltovalidate="TextBoxQty" validationexpression="^[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$" setfocusonerror="true" validationgroup="AddAssests" xmlns:asp="#unknown"></asp:regularexpressionvalidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare='<%# Eval("AvlQty") %>' ControlToValidate="TextBoxQty" ForeColor="Red" ErrorMessage="Must be less than Available" Operator="LessThan" Type="Integer"></asp:CompareValidator>
</ItemTemplate>
<ItemStyle Width="70px"></ItemStyle>
</asp:TemplateField></Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" Enabled="false" Text="Place Order >>" CssClass="btn btn-info" OnClick="Button2_Click" />
If the quantity is greater than the available quantity for every row of the gridview
, the button should be disabled. Otherwise, it should remain enabled.
Currently, I'm looking to implement this functionality using a JavaScript function. Would appreciate any help in achieving this task!
I've written a JavaScript function, but I'm not sure about when and how to call it. Here's what I have so far:
function Calculate() {
var grid = document.getElementById("<%= GridView1.ClientID%>");
var counter = 0;
for (var i = 1; i < grid.rows.length; i++) {
var txtAvl = grid.rows[i].cells[3];
var qty = grid.rows[i].cells[5];
if (txtAvl.value >= qty.value && qty.value > 0) {
counter++;
} else {
document.getElementById('Button2').disabled = true;
}
}
if (counter == grid.rows.length) {
document.getElementById('Button2').disabled = false;
}
}