I want to check and uncheck several ASP checkboxes with the event of other ASP checkbox.
I want that the checkbox with ID "chkSelecAllGtias" can check and uncheck the checkboxes of the "GridView" with the ID "chkSeleccionarGtia"
Here my ASP code:
<div id="divGtiasLn" runat="server">
<div class="formRow" id="tLineasG" style="padding:10px 20px 20px 20px">
<label style="font-weight:bold">Título de la cabecera</label>
<div class="BotonesOpcion AlineaDerecha">
<div class="label">
Seleccionar Todas
<asp:CheckBox id='chkSelecAllGtias' runat='server'></asp:CheckBox>
</div>
</div>
<div style="width:100%;max-height:350px;overflow:auto">
<asp:GridView ID="rgDatosGtias" runat="server" AutoGenerateColumns="false" EmptyDataText="No se encontraron" DataKeyNames="DataField1,DataField2,DataField3,DataField4" AlternatingRowStyle-BackColor="#DAE2E8">
<Columns>
<asp:TemplateField HeaderText="Seleccionar">
<ItemTemplate>
<asp:CheckBox ID="chkSeleccionarGtia" runat="server" AutoPostBack="true" OnCheckedChanged="chkSeleccionarGtia_Check" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DataField1" HeaderText="Número1" />
<asp:BoundField DataField="DataField2" HeaderText="Número2" Visible="false" />
<asp:BoundField DataField="DataField3" HeaderText="Número3" />
<asp:BoundField DataField="DataField4" HeaderText="Número4" />
<asp:BoundField DataField="DataField5" HeaderText="Número5" />
<asp:BoundField DataField="DataField6" HeaderText="" Visible="false" />
<asp:BoundField DataField="DataField7" HeaderText="" Visible="false" />
<asp:BoundField DataField="DataField8" HeaderText="" Visible="false" />
</Columns>
</asp:GridView>
</div>
</div>
</div>
My C# code:
protected void Page_Load(object sender, EventArgs e)
{
try
{
chkSelecAllGtias.Attributes.Add("OnClick", "SeleccionarTodasGtias();");
}
catch (Exception ex)
{
msjError.MostrarInfo(ex.InnerException.Message);
}
}
And my JS code:
function SeleccionarTodasGtias() {
console.log("Dentro");
$('#chkSeleccionarGtia').prop('checked', true);
$('#chkSeleccionarGtia').prop('checked', false);
$('#chkSelecAllGtias').prop('checked', true);
$('#chkSelecAllGtias').prop('checked', false);
//$('<=chkSelecAllGtias.ClientID%>').attr('checked', false);
//var objState1 = $find("<%= chkSelecAllGtias.ClientID %>");
console.log("objState1 -> " + objState1);
if (document.getElementById('chkSelecAllGtias').checked == true) {
document.getElementById('chkSeleccionarGtia').checked = true;
alert("true");
}
else {
document.getElementById('chkSeleccionarGtia').checked = false;
alert("false");
}
}
I have tried with only JS but it did not work.
And some combinations of this:
$("#Checkbox1").prop('checked', true); //// To check
$("#Checkbox1").prop('checked', false); //// To un check
And it didn’t work
2
Answers
You can do it like this (my example has check box at botton, but it don’t matter.
So, this:
So, I don’t have your data, but say I have this gridview, and then a check box.
So, this:
Code to load is thus this:
And now we get/see this: