 |
|
 |
Thats exactly what i was looking for! Thanks.
-Arsi
|
|
|
|
 |
|
 |
Object oriented programming provides the ability to extend/limit/modify existing behaviours by inheriting from the class being altered. In this case simply inherit ImageButton, add properties for Disabled, MouseOver and MouseOut then override the OnPreRender method. To handle ViewState implement the IStateManager interface as well and use an internal StateBag to handle storing of the values.
I also notice that you have put code into the finally block in your OnPreRender method. Generally that is not a good idea.
|
|
|
|
 |
|
 |
I've been using this control for a while, so I needed one day to put some javascript code attached to it (specifically, set the focus to the control).
Then, I needed to be able to know the ID of the image. So, I made a few touches to the code and this is my version of it (I'm FBe in the code ):
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
namespace msWebControlsLibrary
{
/// <summary>
/// Summary description for foImageButton.
/// </summary>
[
ToolboxData("<{0}:ExImageButton runat="server"></{0}:ExImageButton>"),
Designer(typeof(ExImageButtonDesigner))
]
public class ExImageButton : System.Web.UI.WebControls.WebControl, INamingContainer
{
private string _strDisableImageURL;
private string _strMouseOverImageURL;
private string _strMouseOutImageURL;
private System.Web.UI.WebControls.ImageButton btnImage;
private System.Web.UI.WebControls.Image btnDisableImage;
// The sole event hook that the button exposes.
public event EventHandler Click;
#region DesignProperties
[
Browsable(true),
Category("Behavior") ]
public bool CausesValidation
{
get
{
return btnImage.CausesValidation;
}
set
{
btnImage.CausesValidation=value;
}
}
[
Bindable(true),
Browsable(true),
Category("Appearance") ]
public string DisableImageURL
{
get
{
return _strDisableImageURL;
}
set
{
_strDisableImageURL = value;
DisableImageURLFromViewState=_strDisableImageURL;
}
}
[
Bindable(false),
Browsable(true),
Category("Appearance") ]
public string MouseOverImageURL
{
get
{
return _strMouseOverImageURL;
}
set
{
_strMouseOverImageURL = value;
MouseOverImageURLFromViewState=_strMouseOverImageURL;
}
}
[
Bindable(false),
Browsable(true),
Category("Appearance") ]
public string MouseOutImageURL
{
get
{
return _strMouseOutImageURL;
}
set
{
_strMouseOutImageURL = value;
MouseOutImageURLFromViewState=_strMouseOutImageURL;
}
}
[Bindable(false), Category("Appearance"), DefaultValue(null),
Description("The text that is displayed inside the button.")]
public string Text
{
get
{
this.EnsureChildControls();
return btnImage.AlternateText;
}
set
{
this.EnsureChildControls();
btnImage.AlternateText = value;
}
}
[Bindable(false), Category("Appearance"), DefaultValue(""),
Description("The URL of the image to be displayed for the button.")]
public string ImageUrl
{
get
{
this.EnsureChildControls();
return btnImage.ImageUrl;
}
set
{
this.EnsureChildControls();
btnImage.ImageUrl = value;
}
}
/// <summary>
/// Added by FBe
/// </summary>
[Bindable(false), Category("Appearance"), DefaultValue(""),
Description("The ID of the img html tag to be displayed for the button when it's enabled.")]
public string EnabledImageID
{
get
{
this.EnsureChildControls();
return btnImage.ID;
}
set
{
this.EnsureChildControls();
btnImage.ID = value;
}
}
/// <summary>
/// Added by FBe
/// </summary>
[Bindable(false), Category("Appearance"), DefaultValue(""),
Description("The ID of the img html tag to be displayed for the button when it's disabled.")]
public string DisabledImageID
{
get
{
this.EnsureChildControls();
return btnDisableImage.ID;
}
set
{
this.EnsureChildControls();
btnDisableImage.ID = value;
}
}
/// <summary>
/// Added by FBe
/// </summary>
/// <remarks>
/// If someday the way that asp.net builds the child control ids change, this property will stop working right.
/// </remarks>
[Bindable(false), Category(""), DefaultValue(""),
Description("The definitive ID of the img html tag to be displayed for the button when it's enabled.")]
public string RuntimeEnabledImageID
{
get
{
this.EnsureChildControls();
if(btnImage.ID != null && btnImage.ID != string.Empty)
return this.ID + "_" + btnImage.ID;
else
return string.Empty;
}
}
/// <summary>
/// Added by FBe
/// </summary>
/// <remarks>
/// If someday the way that asp.net builds the child control ids change, this property will stop working right.
/// </remarks>
[Bindable(false), Category(""), DefaultValue(""),
Description("The definitive ID of the img html tag to be displayed for the button when it's disabled.")]
public string RuntimeDisabledImageID
{
get
{
this.EnsureChildControls();
if(btnDisableImage.ID != null && btnDisableImage.ID != string.Empty)
return this.ID + "_" + btnDisableImage.ID;
else
return string.Empty;
}
}
#endregion
#region DesignPropertiesViewStates
protected string DisableImageURLFromViewState
{
get
{
return (string) ViewState["DisableImageURLFromViewState"];
}
set
{
ViewState["DisableImageURLFromViewState"] = value;
}
}
protected string MouseOverImageURLFromViewState
{
get
{
return (string) ViewState["MouseOverImageURLFromViewState"];
}
set
{
ViewState["MouseOverImageURLFromViewState"] = value;
}
}
protected string MouseOutImageURLFromViewState
{
get
{
return (string) ViewState["MouseOutImageURLFromViewState"];
}
set
{
ViewState["MouseOutImageURLFromViewState"] = value;
}
}
#endregion
override protected void OnInit(EventArgs ec)
{
base.OnInit(ec);
}
override protected void OnLoad(EventArgs eo)
{
}
override protected void OnPreRender(EventArgs ecx)
{
try
{
btnDisableImage.ImageUrl=DisableImageURLFromViewState;
btnImage.Attributes.Remove("OnMouseOver");
btnImage.Attributes.Remove("OnMouseOut");
}
finally
{
if(MouseOverImageURLFromViewState!=null) btnImage.Attributes.Add("OnMouseOver", "this.src='"+ MouseOverImageURLFromViewState +"';");
if(MouseOutImageURLFromViewState!=null) btnImage.Attributes.Add("OnMouseOut", "this.src='"+ MouseOutImageURLFromViewState +"';");
}
if(Enabled)
{
btnImage.Visible=true;
btnDisableImage.Visible=false;
}
else
{
btnImage.Visible=false;
btnDisableImage.Visible=true;
}
}
protected override void CreateChildControls()
{
// Setup the controls on the page.
btnImage = new System.Web.UI.WebControls.ImageButton();
btnDisableImage =new System.Web.UI.WebControls.Image();
btnDisableImage.ImageUrl=DisableImageURLFromViewState;
btnDisableImage.Visible=false;
// Setup the events on the page.
btnImage.Click += new ImageClickEventHandler(this.OnBtnImage_Click);
Controls.Add(btnImage);
Controls.Add(btnDisableImage);
}
protected virtual void OnBtnImage_Click(object sender, ImageClickEventArgs e)
{
if (Click != null && Enabled )
{
Click(this, e);
}
}
}
public class ExImageButtonDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
ExImageButton cb = (ExImageButton) Component;
if(cb.Controls.Count == 0)
cb.Text = cb.UniqueID;
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
cb.RenderBeginTag(tw);
cb.RenderControl(tw);
cb.RenderEndTag(tw);
return(sw.ToString());
}
}
}
|
|
|
|
 |
|
 |
Here is my version: public class ExImageButton : ImageButton { private string _strDisableImageURL; private string _strEnableImageURL; private string _strMouseOverImageURL; private string _strMouseOutImageURL; /// <summary> /// ~ is supported! /// </summary> #region properties [Bindable(true), Browsable(true), Category("Appearance") ] public string DisableImageURL { get { return _strDisableImageURL; } set { _strDisableImageURL = value; DisableImageURLFromViewState = _strDisableImageURL; } } /// <summary> /// Always provide aboslute path, ~ is not supported /// </summary> [Bindable(false), Browsable(true),Category("Appearance") ] public string MouseOverImageURL { get { return _strMouseOverImageURL; } set { _strMouseOverImageURL = value; MouseOverImageURLFromViewState = _strMouseOverImageURL; } } /// <summary> /// Always provide aboslute path, ~ is not supported /// </summary> [Bindable(false), Browsable(true),Category("Appearance") ] public string MouseOutImageURL { get { return _strMouseOutImageURL; } set { _strMouseOutImageURL = value; MouseOutImageURLFromViewState=_strMouseOutImageURL; } } [Bindable(false), Category("Appearance"), DefaultValue(""), Description("The URL of the image to be displayed for the button.")] public override string ImageUrl { get { return _strEnableImageURL; } set { _strEnableImageURL = value; EnableImageURLFromViewState = _strEnableImageURL; } } #endregion #region DesignPropertiesViewStates protected string EnableImageURLFromViewState { get { return (string)ViewState["EnableImageURLFromViewState"]; } set { ViewState["EnableImageURLFromViewState"] = value; } } protected string DisableImageURLFromViewState { get { return (string) ViewState["DisableImageURLFromViewState"]; } set { ViewState["DisableImageURLFromViewState"] = value; } } protected string MouseOverImageURLFromViewState { get { return (string) ViewState["MouseOverImageURLFromViewState"]; } set { ViewState["MouseOverImageURLFromViewState"] = value; } } protected string MouseOutImageURLFromViewState { get { return (string) ViewState["MouseOutImageURLFromViewState"]; } set { ViewState["MouseOutImageURLFromViewState"] = value; } } override protected void OnPreRender(EventArgs ecx) { try { this.Attributes.Remove("OnMouseOver"); this.Attributes.Remove("OnMouseOut"); } finally { if (!string.IsNullOrEmpty(MouseOverImageURLFromViewState)) this.Attributes.Add("OnMouseOver", "this.src='" + MouseOverImageURLFromViewState + "';"); if (!string.IsNullOrEmpty(MouseOutImageURLFromViewState)) this.Attributes.Add("OnMouseOut", "this.src='" + MouseOutImageURLFromViewState + "';"); } if (Enabled) { string url = EnableImageURLFromViewState; if (string.IsNullOrEmpty(url)) this.Visible = false; else { this.ImageUrl = url; this.Visible = true; } } else { string url = DisableImageURLFromViewState; if (string.IsNullOrEmpty(url)) this.Visible = false; else { this.ImageUrl = url; this.Visible = true; } } base.OnPreRender(ecx); } #endregion }
|
|
|
|
 |
|
 |
When I use the ExImgeButton (with MouseOverImageURL) with Netscape I always get a login dialog box popup telling me to identify myself...
Anyone know why?
Patrick
|
|
|
|
 |
|
 |
It looks like your control will disable only on the postback. Is there a way to disable the imagebutton right after it has been clicked?
Any ideas would be appreciated.
|
|
|
|
 |
|
 |
you can easily write
yourbutton.Enabled = false;
your button click event.
or in the design time you can do .
|
|
|
|
 |
|
 |
Hello, i´m new in asp.net,how can i copile, mi proyect in asp.net in only one archive????...
|
|
|
|
 |
|
 |
the button images look great, where could we get it?
|
|
|
|
 |
|
 |
Solution includes all files ...
have a nice try ..
|
|
|
|
 |
|
 |
So eloquent. Did you major in English?
--
Shine, enlighten me - shine
Shine, awaken me - shine
Shine for all your suffering - shine
|
|
|
|
 |
|
 |
His bio says Turkey.
Help! I'm an AI running around in someone's f*cked up universe simulator. Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus Every line of code is a liability - Taka Muraoka Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"
|
|
|
|
 |
|
 |
Hehe, I was referring to his first version of the article. The article was then just a heading saying "Introduction", followed by a big chunk of code. It seems that he's working on it though.
Maybe I was a tad mean.
--
Shine, enlighten me - shine
Shine, awaken me - shine
Shine for all your suffering - shine
|
|
|
|
 |