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: Headertemplate> Itemtemplate> asp: templateColumn> Columns> ask: DataGrid> 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: Function selectall (chKVAL, IDVAL) { Var thisfrm = document.forms [0]; / / Find all the elements in Forms For (i = 0; i / / Find Checkbox in the template head IF (idVal.indexof ('chkall')! = -1) { IF (chKVAL == true) { Thisfrm.efficients [i] .checked = true; } Else { Thisfrm.efficients [i] .checked = false; } } // if // If the item except the head is not selected, the selection of the head is canceled. Else IF (IdVal.indexof ('chkdelete')! = -1) { IF (thisfrm.elements [i] .Checked == false) { Thisfrm.efficient [1] .checked = false; } } } // for } // Delete judgment Function Confirmdelete (thisfrm) { For (i = 0; i IF (thisfrm.eferences [i] .name.indexof ('chkdelete')! = - 1) { IF (thisfrm.elements [i] .checked) { Return Confirm ('Do you want to delete the selected record?') } } } } script> In order to let these controls and these scripts we also need to write the following code on the server: Private void grdclient_itemdatabase (object sender, system.web.ui.webcontrols.dataGriditeMeventargs e) { IF (E.Item.ItemType == ListItemType.Header) { CheckBox Chk = (Checkbox) E.Item.FindControl ("Chkall"); Chk.attributes.add ("onclick", "JavaScript: return selectall (this.checked, this.id))); } Else if (E.Item.ItemType == ListItemType.Item) { CheckBox Chk = (Checkbox) E.Item.FindControl ("ChkDelete"); Chk.attributes.add ("onclick", "JavaScript: return selectall (this.checked, this.id))); } } In order to bind these two DataGrid we wrote the following binding method: Private void binddata () { String commandtext = "SELECT AU_ID, AU_LNAME AU_FNAME AS Name, Phone, Address, City, State, Zip from Authors"; DataView DV = SQLHELPER.EXECUtedataSet (ConfigurationString "], CommandType.Text, CommandText) .tables [0] .defaultView; this.grdclient.dataSource = DV; this.grdclient.database (); this.grdserver.dataSource = DV; this.grdserver.database (); } The above code can see that I use the SQLHELPER in Microsoft.ApplicationBlocks.Data namespace, this DLL you can download it from Microsoft's website. Finally, load our delete confirmation event to the client and bound our data when loading, and the code is as follows: Private void Page_load (Object Sender, System.Eventargs E) { / / Place the user code here to initialize the page This.btndelete.attributes.add ("onclick", "Return Confirmdele (this.form);"); IF (! this.ispostback) { THIS.BINDDATA (); } } As for how to delete data, I don't think everyone knows that I am here. If you need this project, please send an email to wu_jian830@hotmail.com, I hope to have a better way can continue. Learning exchange Contact the other Email corresponding to the MSN! In the article, please criticize, thank you!