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.
<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 <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.
Private void Page_Load (object sender, System. EventArgs e)
{
// Put user code to initialize the page here
If (! IsPostBack)
{
Databind ();
}
} Private void databind ()