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
wathertonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TheAvengerCommented:
You need to write this class yourself. Sorry for the bad news, but .Net does not support such functionality by default and you have to create a class which will write the appropriate HTML
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wathertonAuthor Commented:
TheAvenger, thanks for that, know of any good links or maybe an example.?
0
TheAvengerCommented:
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.
0
wathertonAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.