Hyperlink mouseover

  • Thread starter Thread starter Mat
  • Start date Start date
Why this does not work?

======

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load


Me.Hyperlink.Attributes.Add("onmouseover", "ChangeColor()")

End Sub

Private Sub ChangeColor()

Me.Hyperlink.BackColor = Drawing.Color.Yellow

End Sub

======
 
This does not work too, why?

Me.Hyperlink.Attributes.Add("onmouseover", Me.Hyperlink.ForeColor =
Drawing.Color.Black)
 
try css, no code required:

<style>
a:hover {color:red;}
</style>

-- bruce (sqlwork.com)
 
Why this does not work?

It's important that you understand the disconnected architecture in which
web apps run. Once the server has streamed the markup down to the client in
response to a request, it has no further interaction with it until / unless
the client sends it another request. That is why, for example, the server
has no way of telling if the user closes their browser without logging out
"cleanly", etc...

When you add dynamic HTML attributes to a control (e.g. onclick, onmouseover
etc), you're giving it additional instructions to run in response to certain
events once it has been streamed down to the client. If you want these
instructions to be in the form of a piece of code, then that piece of code
also has to be streamed down to the client in the form of client-side
scripting, typically JavaScript.

The reason that your code isn't working is because when you hover your mouse
over the hyperlink, it's trying to run a client-side function called
ChangeColor() - however, there is no client-side script function called
ChangeColor because you have written it in server-side VB.NET...

As Bruce has said, for your particular requirement there is actual need to
use DHTML, as everything you require can be achieved with CSS.
 
Back
Top