Sowmya Sirsi

Sowmya Sirsi

  • NA
  • 173
  • 45.3k

Enable Disable CheckBox in GridView based on condition in ASP.Net

May 20 2021 10:38 AM
Hi Team, 
 
I want to disable the checkbox for both <HeaderTemplate> and  <ItemTemplate>  only if the "Reason" column with data named "hybrid". For the Rest all data checkbox should be enabled. Also, while checking  "hybrid" it should accept both uppercase as well as lowercase.
 
Currently, checkbox is enabled only for "hybrid", not accepting data with "HYBRID" or "Hybrid" 
 
Please do the needfull. 
 
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<asp:GridView ID="gvSearchBlacklist" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" Horizontal CssClass="grid-pad-no-border" Width="570px"
Font-Bold="False" AllowPaging="True" OnPageIndexChanging="gvSearchBlacklist_PageIndexChanging"
OnRowCreated="gvSearchBlacklist_RowCreated" PageSize="10">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="HeaderLevelCheckBox" onclick="javascript:HeaderClick(this);" Enabled="false"/></HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="RowLevelCheckBox" ItemStyle-Wrap="true" Enabled='<%# Eval("Reason").ToString().Equals("hybrid") %>'/></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MSISDN" HeaderText="MSISDN" ItemStyle-Wrap="true">
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="Reason" HeaderText="Reason" ItemStyle-Wrap="true">
<ItemStyle Width="300px" />
</asp:BoundField>
<asp:BoundField DataField="CreatedBy" HeaderText="Created By" ItemStyle-Wrap="true">
<ItemStyle Width="300px" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#507CD1" HorizontalAlign="Center" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center"
Height="30px" />
<PagerStyle BackColor="#507CD1" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" HorizontalAlign="Center" />
</asp:GridView>
<script type="text/javascript">
var TotalChkBx;
var Counter;
window.onload = function () {
//Get total no. of CheckBoxes in side the GridView.
TotalChkBx = parseInt('<%= this.gvSearchBlacklist.Rows.Count %>');
//Get total no. of checked CheckBoxes in side the GridView.
Counter = 0;
}
function HeaderClick(CheckBox) {
//Get target base & child control.
var TargetBaseControl = document.getElementById('<%= this.gvSearchBlacklist.ClientID %>');
var TargetChildControl = "RowLevelCheckBox";
//Get all the control of the type INPUT in the base control.
var Inputs = TargetBaseControl.getElementsByTagName("input");
//Checked/Unchecked all the checkBoxes in side the GridView.
for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox' &&
Inputs[n].id.indexOf(TargetChildControl, 0) >= 0)
Inputs[n].checked = CheckBox.checked;
//Reset Counter
Counter = CheckBox.checked ? TotalChkBx : 0;
}
function ChildClick(CheckBox, HCheckBox) {
//get target control.
var HeaderCheckBox = document.getElementById(HCheckBox);
//Modifiy Counter;
if (CheckBox.checked && Counter < TotalChkBx)
Counter++;
else if (Counter > 0)
Counter--;
//Change state of the header CheckBox.
if (Counter < TotalChkBx)
HeaderCheckBox.checked = false;
else if (Counter == TotalChkBx)
HeaderCheckBox.checked = true;
}
</script>
</td>
</tr>
</table>

Answers (3)