watherton
asked on
asp:ImageButton Rollover
Does anyone know how to achieve the following using a asp:ImageButton.
<a href="#" onMouseover="document.imag es['thepic '].src='im ages/Enter Site_On.gi f'" onMouseout="document.image s['thepic' ].src='ima ges/EnterS ite_Off.gi f'"><img name="thepic" src="images/EnterSite_On.g if"></a>
It needs to be a button so that I can do some checking on the .cs file.
Cheers
Wayne
<a href="#" onMouseover="document.imag
It needs to be a button so that I can do some checking on the .cs file.
Cheers
Wayne
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have something similar. Here is part of the code, which I hope will help you. You need to inherit from the ImageButton class. However I am not sure this will be enough for you, give it a try and check what you need to do more.
protected override void OnPreRender(EventArgs e)
{
this.Attributes ["onmouseout"] = "this.src=" + deselectedImageUrl;
this.Attributes ["onmouseover"] = "this.src=" + selectedImageUrl;
base.OnPreRender (e);
}
Actually you can try to just assign the needed JS code to the onmouseout and onmouseover attributes of an ImageButton, maybe this will also work.
protected override void OnPreRender(EventArgs e)
{
this.Attributes ["onmouseout"] = "this.src=" + deselectedImageUrl;
this.Attributes ["onmouseover"] = "this.src=" + selectedImageUrl;
base.OnPreRender (e);
}
Actually you can try to just assign the needed JS code to the onmouseout and onmouseover attributes of an ImageButton, maybe this will also work.
ASKER
The Avenger I have cracked it.
public class RollOverImageManager
{
private ArrayList mImages;
public RollOverImageManager()
{
mImages = new ArrayList();
}
public void AddImage(string imageUrl)
{
mImages.Add(imageUrl);
}
public void InitialiseImage(Image control,string imageURLMouseOver)
{
InitialiseImage(control,im ageURLMous eOver,cont rol.ImageU rl);
}
public void InitialiseImage(Image control,string imageURLMouseOver,string imageURLMouseOut)
{
AddImage(imageURLMouseOver );
AddAttribute(control.Attri butes, "onMouseOut",imageURLMouse Out);
AddAttribute(control.Attri butes, "onMouseOver",imageURLMous eOver);
}
public void InitialiseImage(HtmlImage control, string imageURLMouseOver)
{
InitialiseImage(control, imageURLMouseOver, control.Src);
}
public void InitialiseImage(HtmlImage control, string imageURLMouseOver, string imageURLMouseOut)
{
AddImage(imageURLMouseOver );
AddAttribute(control.Attri butes, "onMouseOut",imageURLMouse Out);
AddAttribute(control.Attri butes, "onMouseOver",imageURLMous eOver);
}
public void AddAttribute(AttributeColl ection a, string eventName,string imageURL)
{
try
{
a.Remove(eventName);
}
finally
{
a.Add(eventName, "this.src='" + imageURL + "'");
}
}
///<summary>
///Preload the images
///</summary>
public void LoadImages(Page webPage)
{
//Create Javascript
StringBuilder buildString = new StringBuilder();
buildString.Append("\n<SCR IPT LANGUAGE='JavaScript'>\n") ;
buildString.Append("<!--\n ");
buildString.Append("var aImages = new Array();\n");
for(int i = 0; i < mImages.Count; i++)
{
buildString.Append("aImage s[" +
i + "] = new Image();\n");
buildString.Append("aImage s[" +
i + "].src = '"+
mImages[i].ToString() + "';\n");
}
buildString.Append("//-->\ n");
buildString.Append("</SCRI PT>\n");
//Register Client Script
if(webPage == null)
{
throw new ArgumentException("The Page Object is invalid.");
}
if(webPage.Request.Browser .JavaScrip t == true)
{
webPage.RegisterClientScri ptBlock("I mageLoader ",buildStr ing.ToStri ng());
}
}
}
}
Then in the Page_Load of the asp page initialise the images.
private void Page_Load(object sender, System.EventArgs e)
{
//Create an instance of the rollover manager class
RollOverImageManager m = new RollOverImageManager();
//Initialise the image controls
m.InitialiseImage(EnterSit e,"images/ EnterSite_ On.gif");
//Load the images (generate the Javascript)
m.LoadImages(this);
}
And reference those images in the asp page
<asp:ImageButton ID="EnterSite" Runat="server" ImageUrl="images/EnterSite _off.gif"> </asp:Imag eButton>
Hope you find this useful. I found this on a site called Reflection IT http://www.reflectionit.nl/RollOver.aspx
Many thanks goes out to Fons Sonnemans.
Thanks for the pointers TheAvenger.
public class RollOverImageManager
{
private ArrayList mImages;
public RollOverImageManager()
{
mImages = new ArrayList();
}
public void AddImage(string imageUrl)
{
mImages.Add(imageUrl);
}
public void InitialiseImage(Image control,string imageURLMouseOver)
{
InitialiseImage(control,im
}
public void InitialiseImage(Image control,string imageURLMouseOver,string imageURLMouseOut)
{
AddImage(imageURLMouseOver
AddAttribute(control.Attri
AddAttribute(control.Attri
}
public void InitialiseImage(HtmlImage control, string imageURLMouseOver)
{
InitialiseImage(control, imageURLMouseOver, control.Src);
}
public void InitialiseImage(HtmlImage control, string imageURLMouseOver, string imageURLMouseOut)
{
AddImage(imageURLMouseOver
AddAttribute(control.Attri
AddAttribute(control.Attri
}
public void AddAttribute(AttributeColl
{
try
{
a.Remove(eventName);
}
finally
{
a.Add(eventName, "this.src='" + imageURL + "'");
}
}
///<summary>
///Preload the images
///</summary>
public void LoadImages(Page webPage)
{
//Create Javascript
StringBuilder buildString = new StringBuilder();
buildString.Append("\n<SCR
buildString.Append("<!--\n
buildString.Append("var aImages = new Array();\n");
for(int i = 0; i < mImages.Count; i++)
{
buildString.Append("aImage
i + "] = new Image();\n");
buildString.Append("aImage
i + "].src = '"+
mImages[i].ToString() + "';\n");
}
buildString.Append("//-->\
buildString.Append("</SCRI
//Register Client Script
if(webPage == null)
{
throw new ArgumentException("The Page Object is invalid.");
}
if(webPage.Request.Browser
{
webPage.RegisterClientScri
}
}
}
}
Then in the Page_Load of the asp page initialise the images.
private void Page_Load(object sender, System.EventArgs e)
{
//Create an instance of the rollover manager class
RollOverImageManager m = new RollOverImageManager();
//Initialise the image controls
m.InitialiseImage(EnterSit
//Load the images (generate the Javascript)
m.LoadImages(this);
}
And reference those images in the asp page
<asp:ImageButton ID="EnterSite" Runat="server" ImageUrl="images/EnterSite
Hope you find this useful. I found this on a site called Reflection IT http://www.reflectionit.nl/RollOver.aspx
Many thanks goes out to Fons Sonnemans.
Thanks for the pointers TheAvenger.
ASKER