views:

138

answers:

3

Hi guys,

I wan't to register the OnMouseOver and OnMouseOut-Event for an Image from the Code behind, because I must different if the user is logged in or not. Any ideas?

+3  A: 

You can add an attribute to the object.

e.g.

 Image img = new Image();
 img.Attributes.Add("onmouseover", "myjavascriptfunction();");

To set the paramater based on the id of the object, using this:

 Image img = new Image();
 img.Attributes.Add("onmouseover", "myjavascriptfunction(" + img.ClientID + ");");
ck
I must add the parameter of the image-id in the function, so "function(img123)", but because of the runat=Server the ID changes at runtime, what to do?
Kovu
Edited to add what you require.
ck
Works perfectly
Kovu
A: 

On solution would be to use a css class and jQuery:

  <img id="generatedId" class="myHoverImage" />

javascript:

$("img.myHoverImage").mouseover ( function() {

  // you can access the generated id:      

  alert ( this.id );

  // --> your code goes here <-- \\      

});
Ghommey
+1  A: 

Using ck's example, you can achieve what you're trying to do using the ClientID property on your server control. Like this:

yourImage.Attributes.Add("onmouseover", "jsfunction(" + yourImage.ClientID + ");");
Eric Lennartsson