In the Web DataGrid, the color of the row changes when the mouse moves to a row and the row leaves (combined with JavaScript)

Source: Internet
Author: User

Add Javascript in HeadCodeAs follows:

<SCRIPT lang = JavaScript>
Function sel (I) // move the mouse up and then execute
{
Eval (I + ". style. Background = '# cccc66'"); // you can change the color of a row.
Eval (I + ". style. cursor = 'hand'"); // move the cursor up and turn it into a hand shape
}
Function unsel (I) // run after the mouse leaves
{
Eval (I + ". style. Background = ''");
}
Function clicktr (I)
{
Eval (I + ". style. Background = ''");
Window. Open ("Edit. aspx? Param = "+ I," modify "," Height = 490, width = 710, resizable = No, scrollbars = No, status = No, toolbar = No,
Menubar = No, location = No, Left = 50, Top = 50 ");
}
</SCRIPT>

In the itemdatabound event of the DataGrid (when data is bound:

Private void maid (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemtype! = Listitemtype. header)
{
String id = E. item. cells [0]. text; // The first column here is the ID value in data binding (it is convenient to PASS Parameters on the modification page. If there are multiple parameters, you can add them as needed !)
E. Item. Attributes. Add ("ID", ID );
E. Item. Attributes. Add ("onmouseover", "sel (" + ID + ")");
E. Item. Attributes. Add ("onmouseout", "unsel (" + ID + ")");
E. Item. Attributes. Add ("onclick", "clicktr (" + ID + ")");
}
}

********* *************************************//

However, the preceding method is inconvenient. if you add a template column in The DataGrid to provide you with the selection operation (such as deleting the selected column ),
In this case, the above method will cause a new window to pop up every time you select the checkbox (The onclick event is triggered)

Relatively poor solution:

Change the row-based attributes to column-based. Remove the template column and add attributes to all columns.

For example, if the template is listed in the 6th column, you can modify the CS file in this way.

Private void maid (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemtype! = Listitemtype. header)
{
String Bm = E. Item. cells [0]. text;
For (INT I = 0; I <5; I ++)
{
E. Item. cells [I]. Attributes. Add ("ID", "a" + I. tostring () + BM );
E. Item. cells [I]. Attributes. Add ("onmouseover", "sel (" + I. tostring () + "," + BM + ")");
E. Item. cells [I]. Attributes. Add ("onmouseout", "unsel (" + I. tostring () + "," + BM + ")");
E. Item. cells [I]. Attributes. Add ("onclick", "clicktr (" + BM + ")");
}
}
}

In JavaScript code:

Function sel (I, ID)
{
For (var j = 0; j <5; j ++)
{Eval ("A" + J. tostring () + ID + ". style. background = '# cccc66' "); EVAL (" A "+ J. tostring () + ID + ". style. cursor = 'hand '");
}
}
Function unsel (I, ID)
{
For (var j = 0; j <5; j ++)
{Eval ("A" + J. tostring () + ID + ". style. Background = ''");
}
}
Function clicktr (I)
{
For (var j = 0; j <5; j ++)
{
Eval ("A" + J. tostring () + I + ". style. Background = ''");
Window. Open ("Edit. aspx? Param = "+ I," modify "," Height = 490, width = 710, resizable = No, scrollbars = No, status = No, toolbar = No,
Menubar = No, location = No, Left = 50, Top = 50 ");
}

}

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.