user2653652 user2653652 - 2 months ago 6
CSS Question

ASP.NET - JavaScript onmouseout get original background

I added this code into my gridview rowdataBound:

e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E2DED6';this.style.cursor='pointer'")


Now onmouseout I want the original background from the the CSS, I don't know much from JavaScript and jQuery so I would like to keep it to inline JavaScript if possible.

I'm looking for something like this:

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='default'")

Answer

Since you want to keep it inline:

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''")

That should remove the inline style, and default to the normal stylesheet.

You can also be more "sledge hammer", and remove the whole style attribute (this will remove all set inline styles though):

e.Row.Attributes.Add("onmouseout", "this.removeAttribute('style')")

This assumes that you have a css/class set on the element to default to.

An easier way to maintain this would be to use css classes instead:

e.Row.Attributes.Add("onmouseover", "this.className='myHoverClass'")
e.Row.Attributes.Add("onmouseout", "this.className=''")

Define your class in a stylesheet (or in the page):

.myHoverClass {
    background-color: #E2DED6;
    cursor: pointer; 
}