Mixing of DataGrid and Checkbox Cuike519 [original]

zhaozj2021-02-16  56

We know that DataGrid is a very powerful ASP.NET component. We can use it to express a lot of information. You can often see some netizens asked some questions about the control. I am not a master but there is still some of the DataGrid. Learn, plus I prefer to learn, so I will use the combination of DataGrid and Checkbox today to use a simple description. We may encounter this situation when writing: Need to choose all items of a list or cancel all items. Selection to delete these columns and how to give the user a prompt information to be deleted (change the function I have described in the relevant document), I also encountered this problem. I solved it has 2 programs to describe the following: Solution one : 1. Use a page of Checkbox to complete this daunting task (exaggerated), because this method is very simple, I don't write code just to be a simple description. We are on our page DataGrid Place a checkbox control above, it is best to control the location in this way. We can set this Checkbox's autopostback to TRUE. This allows us to submit server events. Obviously we want to use server events Realize this function, followed by traversing all the lines of DataGrid and the selection of Checkbox's Checked. 2. It is still using the server to complete our work, this time some different we will put this Checkbox in the DataGrid corresponding Checkbox The header (header). We add a checkbox control to this template column to complete the same work in the same way, but the process is slightly different. First we need a DataGrid to express our programs, The code on the HTML page is as follows:

// Add the checkbox to the header Chk.checkedChanged = New EventHandler (chK_CHECKEDCHANGED);

}

}

The event handler is as follows:

// Get the Checkbox object of the header of the specified DataGrid

Private Checkbox GetHeaderCheckBox (DataGrid GRD) {

Checkbox chk = NULL;

Foreach (DataGridItem I in Grd.Controls [0] .controls) {

IF (i.ItemType == ListItemType.Header) {

CHK = (Checkbox) I.FindControl ("Chkall Server");

Break;

}

}

Return Chk;

}

Private void chk_checkedchanged (Object sender, system.eventargs e) {

Checkbox chk = this.getHeadercheckBox (this.grdserver);

Foreach (DataGridItem I in this.grdserver.Items) {

CheckBox Inchk = (Checkbox) I.FindControl ("chkdelserver");

Inchk.checked = chk.checked;

}

}

The event handler is the same as all the Checkbox's Checked CHECKED of the DataGrid is the same.

Solution 2: This program corresponds to the server event to describe the client's event. There are also two small slightly different ways.

1. Like the scheme one, he is to support the selection script to the client as to the script below 2 will be described in detail.

2. As with the solution, we still put Checkbox in Header, slightly different is that our use is the client script. In order to achieve this feature, we put a DataGrid on the page as follows:

In order to implement the function of the client script We have to add a JavaScript script for the page, the script code is as follows: