A simple custom control, inheriting from BoundField

I wanted to display Boolean data in a Gridview and didn't like that disabled checkbox look. After searching online, I found some people solved this with Javascript, including the MS AJAX extentions toolkit which has an extender - but I didn't want to have a lot of checkboxes in my html that got replaced on the client's side with javascript that had to loop over them all. I just wanted the images.

The quick-and-dirty way to solve this would have been to create a template column for my Gridview with an image control in it and override the Grid's data binding method to set the ImageUrl property.

But that would have to be done everytime, for every Gridview I'd want to use.

So I wrote a simple custom control, inheriting from BoundField (System.Web.UI.WebControls.BoundField) and now I can use it in any website, in any Gridview.

I liked it - hope you do too.

using System;
using System.Data;

using System.Configuration;

using System.Web;

using System.ComponentModel;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace CustomBoundField

{
    ///<summary>

    /// Summary description for BoundCheckImageField

    ///</summary>
    public class BoundCheckImageField : System.Web.UI.WebControls.BoundField
    {
        #region Properties
 

        ///<summary>

        /// Gets or sets the url of the image to display for a 'true' value

        ///</summary>

        [DefaultValue("")]

        public string TrueValueImageUrl

        {

            get

            {

                object val = base.ViewState["TrueValueImageUrl"];

                if (val != null)

                {

                    return (string)val;

                }

                return string.Empty;

            }

            set

            {

                if (!object.Equals(value, base.ViewState["TrueValueImageUrl"]))

                {
                    base.ViewState["TrueValueImageUrl"] = value;
                    this.OnFieldChanged();
                }

            }

        }

 

        ///<summary>

        /// Gets or sets the url of the image to display for a 'false' value

        ///</summary>

        [DefaultValue("")]

        public string FalseValueImageUrl

        {

            get

            {

                object val = base.ViewState["FalseValueImageUrl"];

                if (val != null)

                {

                    return (string)val;

                }

                return string.Empty;

            }

            set

            {

                if (!object.Equals(value, base.ViewState["FalseValueImageUrl"]))

                {
                    base.ViewState["FalseValueImageUrl"] = value;
                    this.OnFieldChanged();
                }

            }

        }

 
        #endregion
 

        protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)

        {

            //base.InitializeDataCell(cell, rowState);

 

            if (!string.IsNullOrEmpty(this.DataField))

            {

                Image img = new Image();

                img.DataBinding += new EventHandler(this.OnImageDataBind);
                cell.Controls.Add(img);

            }

        }

 

        protected void OnImageDataBind(object sender, EventArgs e)

        {

       

            Control control = (Control)sender;

            Control namingContainer = control.NamingContainer;

            object dataValue = this.GetValue(namingContainer);

 

            if (control is Image)

            {

                if ((Boolean)dataValue)

                {

                    (control as Image).ImageUrl = this.TrueValueImageUrl;

                }
                else
                {

                    (control as Image).ImageUrl = this.FalseValueImageUrl;

                }

            }

            else

            {

                base.OnDataBindField(sender, e);

            }

        }

    }
}
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678