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 solve it has 2 scenarios as follows:
Solution 1:
1. Use a page's Checkbox to complete this daunt task (exaggerated), because this method is very simple, I don't write code just a simple description. We are on the DataGrid on our page. Or let's place a checkbox control below, it is best to use Table to control the location. It looks clearer. We can set this Checkbox's autopostback to true. This allows us to submit server events. It is clear that we want to use server events to implement This feature, which is the same as all the lines of the DataGrid and the selection of Checkbox's Checked.
2. Still using the server's event to complete our work, this time some of the different weckbox put this Checkbox on the header of the column of Checkbox in the DataGrid. We add a checkbox to this template column. The control uses it to complete the same job as the same job, but the process is slightly different. First we need a DataGrid to express our program, the code on the HTML page is as follows:
Headertemplate>
Itemtemplate>
asp: templateColumn>
// Only the main below is not written ... There is a detailed column of binding in GrdClient
ask: DataGrid>
We add a Chkall Server to HerderTemple where Server means that it is called server-side events. We add event code to it when you add events to this control, you must add event code as follows:
Private void grdserver_itemcreated (object sender, system.web.ui.webcontrols.dataGriditeMeventargs e) {
IF (E.Item.ItemType == ListItemType.Header) {
Checkbox Chk = (Checkbox) E.Item.FindControl ("Chkall Server");
// Add a starting event on the Checkbox on 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!