[ASP. NET] Method for Multi-choice submission in the DataGrid

Source: Internet
Author: User
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> ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.