How to do onmouseover/onmouseout events on asp.net button

R

Rob Roberts

I'm using .NET 2.0 and C# on a web site, and I'm trying to use the
onmouseover and onmouseout events to do a rollover effect on an asp.net
button. I've tried manually adding the events to the asp:button tag. The
events I added look like this:

onmouseover="this.className='btnNormal'"
onmouseout="this.className='btnOver'"

This works fine, with the rollover appearing as it should. But when I build
the project in VS2005, I get a validation warning saying "Validation
(ASP.Net): Attribute 'onmouseover' is not a valid attribute of element
'Button', and another one saying the same thing about onmouseout attribute.

So after searching through newsgroup archives, I found recommendations that
this be done by adding the attributes in the Page_Load event, like this:

MyButton.Attributes.Add("onmouseover", "this.className='btnNormal'");
MyButton.Attributes.Add("onmouseout", "this.className='btnOver'");

But I can't get this to work. It compiles just fine, but the events aren't
included in the HTML sent to the browser, and so of course the rollover
effect doesn't work.

What is the best way to handle rollovers like this?

Thanks in advance,
--Rob Roberts
 
F

Fao, Sean

Rob said:
I'm using .NET 2.0 and C# on a web site, and I'm trying to use the
onmouseover and onmouseout events to do a rollover effect on an asp.net
button.

This is the job of JavaScript and/or CSS. Posting back to a server for
these events would put excessive load on your server. Not to mention
that it would be incredibly slow.

HTH,
 
R

Rob Roberts

Fao said:
This is the job of JavaScript and/or CSS. Posting back to a server for
these events would put excessive load on your server. Not to mention that
it would be incredibly slow.

Well, yes, but that's what I'm trying to do. That's why I first tried
adding these event handlers to the button declaration in the aspx file:

onmouseover="this.className='btnNormal'"
onmouseout="this.className='btnOver'"

And while that works just fine in giving the desired rollover effect
(without a postback to the server), VS2005 complains about it, saying that
"Attribute 'onmouseover' is not a valid attribute of element 'Button'".

--Rob Roberts
 
N

Nathan Sokalski

I think that you are simply using an invalid attribute. When using
Javascript to change the CSS Class, you use the 'class' attribute.
Therefore, your code should be:

MyButton.Attributes.Add("onmouseover", "this.class='btnNormal'");
MyButton.Attributes.Add("onmouseout", "this.class='btnOver'");

Hopefully this will solve your problem. Good Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top