DataGrid Click a row to change the background color of this row

Source: Internet
Author: User
Datagrid

DataGrid Click a row to change the background color of this row


1 Set the Alternatingitemstyle.backcolor property of the DataGrid control to "beige".


2 events fired when the DataGrid control creates an item (datagrid1_itemcreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) ), add the following code:

private void Datagrid1_itemcreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (E.item.itemindex >-1)
{
e.item.attributes["name"] = "selecttr";
e.item.attributes["onclick"] = "Selectnode (this)";
}
}


3 Add JavaScript code to the HTML code page as follows:

function Selectnode (objthis)
{
var objtrs = document.getElementsByTagName ("TR");
var j = 0;
for (var i = 0; i < objtrs.length-1 i = i + 2)
{
j = i + 1;
if (Objtrs[i].name = = "Selecttr")
Objtrs[i].style.backgroundcolor = "Beige";
if (Objtrs[j].name = = "Selecttr")
Objtrs[j].style.backgroundcolor = "#ffffff";
}

ObjThis.style.backgroundColor = "#c6ff00";
}

You might as well try! ^_^



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.