I wrote a script that disables textboxes whenever a checkbox is unchecked in a gridview:
$(function () {
//Enable or disable all textboxes when header row checkbox is checked.
$("[id*=chkHeader]").on("click", function () {
var chkHeader = $(this);
//Find and reference the GridView.
var grid = $(this).closest("table");
//Loop through the checkboxes in each row.
$("td", grid).find("input[type=checkbox]").each(function () {
//If the header checkbox is checked, then check all checkboxes and enable the textboxes.
if (chkHeader.is(":checked")) {
$(this).attr("checked", "checked");
var td = $("td", $(this).closest("tr"));
$("input[type=text]", td).removeAttr("disabled");
}
else {
$(this).removeAttr("checked");
var td = $("td", $(this).closest("tr"));
$("input[type=text]", td).attr("disabled", "disabled");
$("input[type=text]", td).val("");
}
});
});
//Enable or disable textboxes in a row when the row checkbox is checked.
$("[id*=chkResult]").on("click", function () {
//Find and reference the GridView.
var grid = $(this).closest("table");
//Find and reference the Header Checkbox.
var chkHeader = $("[id*=chkHeader]", grid);
//If the checkbox is unchecked, then disable the textboxes in that row.
if (!$(this).is(":checked")) {
var td = $("td", $(this).closest("tr"));
$("input[type=text]", td).attr("disabled", "disabled");
$("input[type=text]", td).val("");
}
else {
var td = $("td", $(this).closest("tr"));
$("input[type=text]", td).removeAttr("disabled");
}
//Enable the Header Row Checkbox if all the Row Checkboxes are checked and vice versa.
if ($("[id*=chkResult]", grid).length == $("[id*=chkResult]:checked", grid).length) {
chkHeader.attr("checked", "checked");
}
else {
chkHeader.removeAttr("checked");
}
});
});
The ASP code:
<table>
<tr>
<asp:Panel ID="pnlItems" runat="server" Height="100%">
<td colspan="3" class="PrimaryLabelTop" style="height: auto" >
<asp:Label ID="lbItems" runat="server" CssClass="PrimaryLabel" meta:resourcekey="lblNecropsyFoundResource1">
Necropsy: Itemized Information
</asp:Label>
<br />
<br />
<asp:GridView ID="gvItems" runat="server" EmptyDataText="No data available." Width="100%" DataSourceID="dsItems" EnableModelValidation="True" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ValueID" HeaderText="ValueID" SortExpression="NecropsyValueID" InsertVisible="False" ReadOnly="True" Visible="false" />
<asp:BoundField DataField="GroupE" HeaderText="Group Description" SortExpression="GroupE" ReadOnly="true"/>
<asp:BoundField DataField="ItemE" HeaderText="Item Description" SortExpression="ItemE" ReadOnly="true"/>
<asp:TemplateField HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkResult" runat="server" Checked='<%# IIf(Eval("Result").ToString() = "Selected", True, False) %>' AutoPostBack="True"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment" SortExpression="Commment">
<ItemTemplate>
<asp:TextBox ID="txtComment" Visible='<%# Bind("AllowMemo")%>' runat="server" Text='<%# Bind("Comment") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ValueID" HeaderText="ValueID" InsertVisible="False" ReadOnly="True" SortExpression="ValueID" Visible="false"/>
</Columns>
</asp:GridView>
<br />
<asp:ObjectDataSource ID="dsItems" runat="server" SelectMethod="GetItemValues" TypeName="HealthWeb.HealthWS.Health">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="SampleID" QueryStringField="QS_LABSAMPLE" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</td>
</asp:Panel>
</tr>
<tr>
<td class="PrimaryLabelTop" />
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" meta:resourcekey="btnSaveResource1" height="26px" style="text-align: center" width="60px" />
<asp:Button ID="btnClose" runat="server" Text="Close" meta:resourcekey="btnCloseResource1" height="26px" style="text-align: center" width="60px" />
</td>
</tr>
</table>
Issue 1: The [chkHeader] works correctly. However, when trying to individually check one [chkResult], it clears the [txtComment] but enables all txtComments instead of only the specific row I just checked.
Issue 2: How can I disable textboxes when a checkbox is unchecked from within the gridview?
Thank you!