using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace RolloverButton { /// /// Summary description for WebCustomControl1. /// [DefaultProperty("ImageUrl"), ToolboxData("<{0}:RollOver runat=server>")] public class RollOver : System.Web.UI.WebControls.LinkButton { [Bindable(true), Category("Appearance"), DefaultValue(""), Description("Specifies the image url for the button when in its normal state.")] public string ImageUrl { get { object o = ViewState["RollOverImageUrl"]; if (o != null) return (string) o; else return String.Empty; } set { ViewState["RollOverImageUrl"] = value; } } [Bindable(true), Category("Appearance"), DefaultValue(""), Description("Specifies the image url for the button when the mouse is hovered above the button.")] public string RollOverImageUrl { get { object o = ViewState["RollOverRollOverImageUrl"]; if (o != null) return (string) o; else return String.Empty; } set { ViewState["RollOverRollOverImageUrl"] = value; } } protected override void OnPreRender(EventArgs e) { // Register the client-side script. See this article for more info on adding client-side // script from an ASP.NET server control: // http://msdn.microsoft.com/asp.net/using/understanding/aspnet/default.aspx?pull=/library/en-us/dnaspp/html/aspnet-injectclientsidesc.asp string imageLoadScriptKey = "rolloverImageBuildingCode" + this.ClientID; string script = @""; Page.RegisterClientScriptBlock(imageLoadScriptKey, script); this.Attributes["onmouseover"] = "rolloverServerControl_display('" + this.ClientID + "_img'," + this.ClientID + "_img_over);"; this.Attributes["onmouseout"] = "rolloverServerControl_display('" + this.ClientID + "_img'," + this.ClientID + "_img_out);"; const string displayScriptKey = "rolloverImageLoadScript"; if (!Page.IsClientScriptBlockRegistered(displayScriptKey)) { script = @""; Page.RegisterClientScriptBlock(displayScriptKey, script); } base.OnPreRender (e); } protected override void RenderContents(HtmlTextWriter writer) { if (this.ImageUrl != String.Empty) { Image myImage = new Image(); myImage.Attributes["name"] = this.ClientID + "_img"; myImage.ImageUrl = this.ImageUrl; myImage.AlternateText = this.Text; myImage.Height = this.Height; myImage.Width = this.Width; myImage.ToolTip = this.ToolTip; myImage.RenderControl(writer); } else base.RenderContents (writer); } } }