ASP. NET and JavaScript operations on the checkbox control

Source: Internet
Author: User
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.

Related Article

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.