If you are working on a web project where there are a lot of image buttons that need to have rollover functionality you can extend the ImageButton control as demonstrated below. It takes less than 30 lines of code, but it makes a project much easier to maintain because the code for the rollovers is implemented in one place instead of in each seperate page so you don't have to dig through html to manage your rollovers.
I hope you find this helpful.
Until next time,
Happy coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:RolloverImageButton runat=server></{0}:RolloverImageButton>")]
public class RolloverImageButton : ImageButton
{
[DefaultValue("")]
[UrlProperty]
[Bindable(true)]
public virtual string ImageOverUrl
{
get
{
if (null == ViewState["ImageOverUrl"]) return string.Empty;
else return Convert.ToString(ViewState["ImageOverUrl"]);
}
set { ViewState["ImageOverUrl"] = value; }
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute("onmouseover", "this.src='" + base.ResolveClientUrl(ImageOverUrl) + "'");
writer.AddAttribute("onmouseout", "this.src='" + base.ResolveClientUrl(ImageUrl) + "'");
base.AddAttributesToRender(writer);
}
}
}
pazhani velPosted Mar 15, 2009, 2:03 PM
hello... i am developing mobile application. how do i create rollover images when i click on any images.
Neil JenningsPosted Nov 14, 2007, 4:10 PM
this control is brill but ran into a problem when I try and use on a master page - when the page used in in a higher directory than the root {where the master is } the image's src becomess ..\images which cause a fatal error i.e the src is expected as ~/images or even images {could copy dir} Can not spot code to change - not sure if to try and create an adapter to render the control or try for another control
bim kanPosted Aug 27, 2007, 6:32 AM
i want to learn C# from which point that i should start to learn this language i will be second year computer engineering student next year i am waiting for your important advices nice to meet you . Celal ,Turkey [email protected]
AlexandrePosted Mar 8, 2007, 7:43 AM
hello, I would like tu use the Rollover Image Button Class but I don' t know how to call the class in ASP.NET2 pages. Can You explain how to do that? Greetings Alex