?
Solved

asp:ImageButton Rollover

Posted on 2004-11-05
4
Medium Priority
?
795 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:watherton
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 12503478
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
 

Author Comment

by:watherton
ID: 12503516
TheAvenger, thanks for that, know of any good links or maybe an example.?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12503638
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
 

Author Comment

by:watherton
ID: 12505290
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question