Idea: Create a template column in The DataGrid and use the checkbox and checkboxlist controls for submission. The checkbox control is used for full selection. The checkboxlist control records the product numbers and collects submitted numbers cyclically.
1. Create a template column in The DataGrid, open the template, put a checkbox control in the header, and rename it selall. Put a checkboxlist control in item and change it to selnum.
2. Set the autopostback attribute of the selall control to true.
3. Bind the item ID to the checkboxlist control and add it to the itemdatabound event of the DataGrid:
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem)
{
Checkboxlist chklist = (checkboxlist) E. Item. cells [1]. findcontrol ("selnum ");
Chklist. items [0]. Text = "";
Chklist. items [0]. value = convert. tostring (databinder. eval (E. Item. dataitem, "detailid "));
}
We only need a component of the checkboxlist control.
4. Create the selall checkedchanged event in the itemcreated event of the DataGrid:
If (E. Item. itemtype = listitemtype. header)
{
Checkbox selall = (checkbox) E. Item. findcontrol ("selall ");
Selall. checkedchanged + = new eventhandler (selall_checkedchanged );
}
Method for adding the checkedchanged event of selall:
1 protected void selall_checkedchanged (Object sender, system. eventargs E)
2 {
3
4 checkbox chk = This. getheadercheckbox (this. datagrid1 );
5
6 foreach (maid I in this. Maid)
7 {
8
9 checkboxlist inchk = (checkboxlist) I. findcontrol ("selnum ");
10
11 if (I. cells [0]. Enabled = true)
12 inchk. items [0]. Selected = chk. checked;
13
14}
15
16}
Row 4 uses the getheadercheckbox Method to Determine the selected status.
Private checkbox getheadercheckbox (DataGrid DG)
{
Checkbox chk = NULL;
Foreach (maid I in DG. controls [0]. Controls)
{
If (I. itemtype = listitemtype. header)
{
Chk = (checkbox) I. findcontrol ("selall ");
Break;
}
}
Return chk;
}
5. Finally, we can click a button to obtain the id value we need. Add the following to the Click Event of the button:
For (INT I = 0; I <this. datagrid1.items. Count; I ++)
{
Checkboxlist chklist = (checkboxlist) This. Maid [I]. cells [1]. findcontrol ("selnum ");
If (chklist. items [0]. Selected)
{
Response. Write (chklist. items [0]. value );
Response. Write ("<br> ");
}
}