smart developer’s blog

This is a C# resource library! Free how to’s and best practices…

How to make a CheckBox fire the ItemCommand Event of a Repeater

leave a comment »

Today I needed to add a check box control to a repeater’s item template and have it fire the ItemCommand event on the repeater.  I could’t do that, because this control (Checkbox) does not trigger this event. So, I searched the Internet and I found this cool new control:


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace smartdev.web.Controls
{
    public class CheckBoxRepeaterAware : CheckBox
    {
        #region Properties

        #region CommandName

        public string CommandName
        {
            get
            {
                if (this.ViewState["CommandName"] == null)
                {
                    return string.Empty;
                }
                else
                {
                    return this.ViewState["CommandName"] as string;
                }
            }
            set
            {
                this.ViewState["CommandName"] = value;
            }
        }

        #endregion

        #region CommandArgument

        public string CommandArgument
        {
            get
            {
                if (this.ViewState["CommandArgument"] == null)
                {
                    return string.Empty;
                }
                else
                {
                    return this.ViewState["CommandArgument"] as string;
                }
            }
            set
            {
                this.ViewState["CommandArgument"] = value;
            }
        }

        #endregion

        #endregion

        #region Procedures

        #region On Checked Changed

        protected override void OnCheckedChanged(EventArgs e)
        {
            //create a new event args of type command event args
            CommandEventArgs ce = new CommandEventArgs(this.CommandName, this.CommandArgument);

            //allow the base checkbox to handle the event as normal
            base.OnCheckedChanged(e);

            //raise the contorls method RaiseBubbleEvent
            base.RaiseBubbleEvent(this, ce);
        }
        #endregion

        #endregion
    }
}

I hope you will find this usefull.

Advertisements

Written by smartdev

April 4, 2009 at 1:17 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: