Hyperlink Rollover images in Repeater error

P

Patrick.O.Ige

I want to have a rollover image on an hyperlink inside a Repeater control
but when i run the page i get
"Image1 is undefined"
Any ideas?

I have a Hyperlink and an Image control in a repeater like below:-
<ItemTemplate>
<tr width="150px" style="cursor:hand"
onmouseover="style.backgroundColor='#c0c0c0';"
onmouseout="style.backgroundColor=''" bordercolor="#000000">
<td>
<asp:Image id="Image1" runat="server"
ImageUrl="images/off.gif"></asp:Image>
<asp:HyperLink CssClass="a" id="HyperLink1" Text='<%#
DataBinder.Eval(Container.DataItem, "cat_cities") %>' NavigateUrl='<%#
"template2.aspx?CategoryID=" & DataBinder.Eval(Container.DataItem, "catid")
%>' runat="server" />
</td>
</tr>
</ItemTemplate>
And in the repeater i did:-
Sub Repeater_ItemCreated(ByVal sender As Object, ByVal e As
RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim hyperLink As HyperLink =
CType(e.Item.FindControl("HyperLink1"), HyperLink)
Dim Image As System.Web.UI.WebControls.Image =
CType(e.Item.FindControl("Image1"), System.Web.UI.WebControls.Image)

hyperLink.Attributes.Add("onMouseOver", Image.ClientID &
".src='images/up.gif';return true;")
hyperLink.Attributes.Add("onMouseOut", Image.ClientID &
".src='images/off.gif'; return true;")


End If
End Sub
 
N

Nathan Sokalski

When using WebControls to create elements such as images, the generated code
does not include a name attribute unless you include a name property. You
need to add a name="Image1" attribute to your asp:Image tag as follows:

<asp:Image id="Image1" name="Image1" runat="server"
ImageUrl="images/off.gif"></asp:Image>

Good Luck!
 
N

Nathan Sokalski

One thing that I might try looking at is view generated HTML by doing a
View -> Source from your browser to make sure the tags in the generated code
have the same name that you think they do. Sometimes in the Repeater,
DataList, and DataGrid Controls the name attribute is modified to allow
Javascript to distinguish between the controls.
 
R

Rote Rote

Yeah i did that and i know that.
Thanks Nathan would do one more thing i think could make it work and
would let you know.
Thanks for the advice
 
R

Rote Rote

Nathan i got it fixed i used
hyperLink.Attributes.Add("onMouseOver", "document.getElementById('" &
Image.ClientID & "').src='images/up.gif';return true;")
hyperLink.Attributes.Add("onMouseOut",
"document.getElementById('" & Image.ClientID & "').src='images/off.gif';
return true;")
in the ItemDataBound of my Repeater and it worked like a charm although
it might be a good idea adding the name attirbute to the image control
but still worked just by using the ID.
Patrick
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top