I have a GridView in asp.net page like below:
Checkbox Name Link
[] MainName myLink
[] -somename1 myLink1
[] -somename2 myLink2
[] --somename2.1 myLink2.1
[] MainName2 mylink3
[] -somename3 Mylink4
Note that in the naming pattern, the "Main Titles" do not include the dash character as a starting character, while the other titles have.
What I want is that, when I click the checkbox near "MainName" I need all the sub rows that belongs to the selected MainName will also be selected automatically. Assuming I click the checkbox at the first row: MainName, then it should be seen:
Checkbox Name Link
[checked] MainName myLink
[checked] -somename1 myLink1
[checked] -somename2 myLink2
[checked] --somename2.1 myLink2.1
[] MainName2 mylink3
[] -somename3 Mylink4
I am managing it by using C# like below:
protected void SelectCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
System.Web.UI.WebControls.CheckBox chk = sender as System.Web.UI.WebControls.CheckBox;
GridViewRow row = (GridViewRow)chk.NamingContainer;
bool checkVal = false;
String cellText = row.Cells[2].Text;
if (cellText.IndexOf('-') < 0)
{
foreach (GridViewRow r in dgMenuItems.Rows)
{
if (row == r)
{
checkVal = true;
}
if (checkVal)
{
if (r.Cells[2].Text.IndexOf('-') < 0)
{
break;
}
else
{
var cb = (System.Web.UI.WebControls.CheckBox)r.FindControl("chbSelect");
cb.Checked = true;
}
}
}
}
}
But I want to handle it in JavaScript or JQuery, not in the server side. Any help or advice in this regard will be appreciated.
This is one way to do it.
The GridView with source for demo
And the code behind