A few years ago I posted ASP.NET custom control HyperLinkWithImage. I also created similar CheckBoxWithImage , but didn't publish it at that time.
The class allows to have image next to CheckBox, and by clicking image check-box will be ticked/unticked.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design; //!!!System.Design (in System.Design.dll)
using System.Drawing.Design;
using System.Collections.Specialized;//NameValueCollection Class
using System.Diagnostics;
using FSHelperLib;
/// <summary>
/// Summary description for CheckBoxWithImage.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:CheckBoxWithImage runat=server></{0}:CheckBoxWithImage>")]
public class CheckBoxWithImage : System.Web.UI.WebControls.CheckBox
{ //,IPostBackDataHandler
private string m_ImageUrl;
private ImageAlign m_ImageAlign;
public CheckBoxWithImage() : base()
{
//#if DEBUG
// base.CheckedChanged += new System.EventHandler(this.OnCBCheckedChanged);
//#endif
}
//#if DEBUG
// void OnCBCheckedChanged(object sender, EventArgs e)
// {
// if (Checked == true)
// Debug.WriteLine( "OnCBCheckedChanged true");
// else
// Debug.WriteLine( "OnCBCheckedChanged false");
// }
//#endif
[Bindable(true),
Category("Appearance"),
DefaultValue(""),
EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor))]
public string ImageUrl
{
get
{
return m_ImageUrl;
}
set
{
m_ImageUrl = value;
}
}
//WebCategory("Layout")-internal in System.Web
//WebSysDescription("Image_ImageAlign")-internal in System.Web
[ Bindable(true), Category("Layout"), DefaultValue(0)]
public virtual ImageAlign ImageAlign
{
get
{
object obj1 = m_ImageAlign ;//this.ViewState["ImageAlign"];
if (obj1 != null)
{
return ((ImageAlign) obj1);
}
return ImageAlign.NotSet;
}
set
{
if ((value < ImageAlign.NotSet) || (value > ImageAlign.TextTop))
{
throw new ArgumentOutOfRangeException("value");
}
m_ImageAlign= value;// this.ViewState["ImageAlign"] = value;
}
}
/// <summary>
/// Overrides the OnPreRender method.
/// </summary>
protected override void OnPreRender(System.EventArgs e)
{
base.OnPreRender(e);
EWSoftware.Web.ResSrvHandler.RegisterStartupScript(this.Page,"ToggleCheckBox.js");
ClientScriptsHelper.RegisterScript_FindObjById(this.Page); //EWSoftware.Web.ResSrvHandler.RegisterStartupScript(,"FSHelperLib","FindObjById");
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
string sUrl, sImg=null;
if (!DataHelper.IsNullOrEmpty(m_ImageUrl))
{
sUrl=this.ResolveUrl(m_ImageUrl);//to resolve '~' can't be called in construction time
sImg="<IMG alt=\"\" src=\"" + sUrl + "\" onclick=\"ToggleCheckBox('" + this.ClientID + "')\" >";
}
bool bImageRight=(ImageAlign.Right== m_ImageAlign);
if ((null!=sImg) && (!bImageRight))
{
output.Write(sImg);
}
base.Render(output);
if ((null!=sImg) && (bImageRight))
{
output.Write(sImg);
}
}
// #region Implementation of IPostBackDataHandler -not required
// void IPostBackDataHandler.RaisePostDataChangedEvent()
// {
// base.OnCheckedChanged( System.EventArgs.Empty);
// }
// bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
// { //Exact copy of base implementation
// string text1 = postCollection[postDataKey];
// bool flag1 = (text1 == null ? false : text1.Length > 0);
// bool flag2 = flag1 != this.Checked;
// this.Checked = flag1;
// return flag2;
// }
//
//// bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
//// { CheckBox bs=this;
//// IPostBackDataHandler ip=(IPostBackDataHandler)bs;
//// return ip.LoadPostData(postDataKey,postCollection);
//// }
// #endregion
}