Link to home
Start Free TrialLog in
Avatar of watherton
watherton

asked on

asp:ImageButton Rollover

Does anyone know how to achieve the following using a asp:ImageButton.

<a href="#" onMouseover="document.images['thepic'].src='images/EnterSite_On.gif'" onMouseout="document.images['thepic'].src='images/EnterSite_Off.gif'"><img name="thepic" src="images/EnterSite_On.gif"></a>


It needs to be a button so that I can do some checking on the .cs file.

Cheers

Wayne
ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of watherton
watherton

ASKER

TheAvenger, thanks for that, know of any good links or maybe an example.?
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.
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,imageURLMouseOver,control.ImageUrl);
            }
            public void InitialiseImage(Image control,string imageURLMouseOver,string imageURLMouseOut)
            {
                  AddImage(imageURLMouseOver);
                  AddAttribute(control.Attributes, "onMouseOut",imageURLMouseOut);
                  AddAttribute(control.Attributes, "onMouseOver",imageURLMouseOver);
            }
            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.Attributes, "onMouseOut",imageURLMouseOut);
                  AddAttribute(control.Attributes, "onMouseOver",imageURLMouseOver);
            }
            public void AddAttribute(AttributeCollection 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<SCRIPT LANGUAGE='JavaScript'>\n");
                  buildString.Append("<!--\n");
                  buildString.Append("var aImages = new Array();\n");
                  for(int i = 0; i < mImages.Count; i++)
                  {
                        buildString.Append("aImages[" +
                              i + "] = new Image();\n");
                        buildString.Append("aImages[" +
                              i + "].src = '"+
                              mImages[i].ToString() + "';\n");

                  }
                  buildString.Append("//-->\n");
                  buildString.Append("</SCRIPT>\n");

                  //Register Client Script
                  if(webPage == null)
                  {
                        throw new ArgumentException("The Page Object is invalid.");
                  }
                  if(webPage.Request.Browser.JavaScript == true)
                  {
                        webPage.RegisterClientScriptBlock("ImageLoader",buildString.ToString());
                  }
            }
                                                      

      }
}

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(EnterSite,"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:ImageButton>


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.