Author: Leng Feng Source: CSDN
DataGrid Control:
<Columns>
<Asp: TemplateColumn>
<HeaderStyle Width = "10px"> </HeaderStyle>
<HeaderTemplate>
<INPUT id = "CheckAll" name = "CheckAll" type = "checkbox" onclick = "ChooseAll ()">
</HeaderTemplate>
<ItemTemplate>
<Asp: CheckBox id = "CheckBox1" runat = "server"> </asp: CheckBox>
</ItemTemplate>
</Asp: TemplateColumn>
<Asp: TemplateColumn HeaderText = "no.">
<HeaderStyle Wrap = "False"> </HeaderStyle>
<ItemTemplate>
<Asp: Label runat = "server" ID = "labNO"> </asp: Label>
</ItemTemplate>
</Asp: TemplateColumn>
JavaScript Functions:
Var checkFlag = true;
Function ChooseAll ()
{
// If (! Document. all ("CheckAll"). Checked) // select all
If (checkFlag) // select all
{
Var inputs = document. all. tags ("INPUT ");
For (var I = 0; I <inputs. length; I ++) // traverses all input values on the page
{
If (inputs [I]. type = "checkbox" & inputs [I]. id! = "CheckAll ")
{
Inputs [I]. checked = true;
}
}
CheckFlag = false;
}
Else // cancel all selections
{
Var inputs = document. all. tags ("INPUT ");
For (var I = 0; I <inputs. length; I ++) // traverses all input values on the page
{
If (inputs [I]. type = "checkbox" & inputs [I]. id! = "CheckAll ")
{
Inputs [I]. checked = false;
}
}
CheckFlag = true;
}
}
// <Summary>
// Ask the user to confirm and delete the data.
/// </Summary>
Function DelRec ()
{
Var inputs = document. all. tags ("input ");
Var selectedLen = 0;
For (var I = 0; I <inputs. length; I ++)
{
If (inputs [I]. type = "checkbox ")
{
If (inputs [I]. checked)
{
If (inputs [I]. id! = "CheckAll ")
{
SelectedLen ++;
}
}
}
}
If (selectedLen = 0)
{
Alert ("select the data you want to delete first! ");
}
Else
{
Var flag = confirm ("are you sure you want to delete the selected" + selectedLen + "data? ");
If (flag)
{
Document. all ("BtnDelRecord"). click ();
}
}
}