Search code examples
jqueryasp.netaspxgridview

Make text box read only in a asp.net grid row based on checked columns


In the below grid, I wanted to disable textbox in each row if all the checkboxes in the row are unchecked. And if a check box is checked in a row then need to enable the textbox.

Wanted this in client side using jquery. Please help Below is the code I have written. Some code line are repeating how to optimize it more ??

 $(function () {
             $('#MainContent_G2 > tbody  > tr').each(function (index, row) {
                 var checkedArr = $(row).find('input[type="checkbox"]:checked');
                 if (checkedArr.length === 0) {
                     var textbox = $(row).find('input[type="text"]');
                     $(textbox).attr('readonly', true);
                 }

                 $(row).find('input[type="checkbox"]').each(function(index, checkbox) {
                     $(checkbox).change(function () {
                         if (this.checked) {
                             $(this).closest("tr").find('input[type="text"]').attr('readonly', false);
                         }

                         var checkedArr = $(row).find('input[type="checkbox"]:checked');
                         if (checkedArr.length === 0) {
                             var textbox = $(row).find('input[type="text"]');
                             $(textbox).attr('readonly', true);
                         }
                     });
                 });

             });
         });
<asp:GridView ID="G2" ShowHeader="False" runat="server" AutoGenerateColumns="False" OnRowDataBound="G2_RowDataBound">
  <HeaderStyle BorderStyle="Solid" HorizontalAlign="Left" BackColor="LightGray" />
  <FooterStyle BorderWidth="1px" BorderStyle="Solid" BackColor="LightGray" />
  <RowStyle CssClass="gridRows" />
  <Columns>
    <asp:BoundField DataField="REVIEWQUESTIONS_DESC" />
    <asp:TemplateField>
      <ItemTemplate>
        <asp:CheckBox ID="chkbox_Yes" runat="server" Checked='<%# Eval("ANSWER_YES")%>' />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:CheckBox ID="chkbox_No" runat="server" Checked='<%# Eval("ANSWER_NO")%>' />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:CheckBox ID="chkbox_NA" runat="server" Checked='<%# Eval("ANSWER_NA")%>' />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:TextBox runat="server" CssClass="NoLine" ID="txtbox_Notes" Text='<%# Eval("INCIDENTREVIEW_NOTES")%>'></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>


Solution

  • You could do something like this:

    Text='<% # Eval("NameYourUsing") %>' Enabled="false"