In network development, it is often necessary to use ASP. NET and JavaScript For joint control. In this article, we will use the DataGrid for Data Binding and use JavaScript to control the color of the row when the checkbox is selected.
First, create a DataGrid Control on the page and set its template.
The following is a reference clip:
<Asp: DataGrid id = "datagrid1" runat = "server" autogeneratecolumns = "false"> <Columns> <Asp: templatecolumn> <Itemtemplate> <Asp: checkbox id = "checkbox1" runat = "server"> </ASP: checkbox> <Asp: Label runat = "server" text = '<% # databinder. eval (container, "dataitem") %>'> </ASP: Label> </Itemtemplate> </ASP: templatecolumn> </Columns> </ASP: DataGrid> |
Second, compile the Javascript script function in
The following is a reference clip:
<SCRIPT> Function checkme (OBJ, TR ){ If (obj. Checked) Tr. style. backgroundcolor = 'blue '; Else Tr. style. backgroundcolor = ''; } </SCRIPT> |
Third, bind data to the DataGrid in the page_load event and associate it with the Javascript script of the checkbox.
The following is a reference clip:
private void page_load (Object sender, system. eventargs E) { // put user code to initialize the page here If (! Ispostback) { databind (); } private void databind () { arraylist arr = new arraylist (); arr. add ("News synthesis"); arr. add (""); arr. add ("movie"); arr. add ("education"); arr. add ("drama"); arr. add ("military"); arr. add ("Sports"); datagrid1.datasource = arr; datagrid1.databind (); int I; for (I = 0; I checkbox CB; cb = (checkbox) datagrid1.items [I]. findcontrol ("checkbox1"); datagrid1.items [I]. attributes. add ("ID", "TR" + I. tostring (); CB. attributes. add ("onclick", "checkme (this, TR" + I. tostring () + ");"); } |
fourth, run the Program . After the program runs, a checkbox control is displayed in front of each row of the DataGrid Control. Select this control. The background color of this row changes to blue. deselect the control and the color of this row is restored to the initial state.