Command-capable CheckBox with GridView

Posted by: Jotekes Blog, on 13 Oct 2006 | View original | Bookmarked: 0 time(s)

Ben asked on newsgroups:

"I have a GridView control with a dynamically created checkbox column.  The 
checkbox is created using ITemplate interface.  In my class that implements 
the ITemplate interface I set the AutoPostBack property of the checkbox to 
True.  When the checkbox is clicked, I want to handle the CheckChanged event 
of the checkbox on the webform (.aspx) that contains the GridView control. 
I can easily use the addhandler instruction in the ITemplate interface class 
to handle the CheckChanged event, but how do I bubble the event out to the 
actual .aspx page that contains the gridview control????????????"

Reply: One way is to develop a custom CheckBox control which literally bubbles the Command event up

public class CommandCheckBox : System.Web.UI.WebControls.CheckBox
{

    #region "Command event implementation"
    private static readonly object EventCommand = new object();

    public event CommandEventHandler Command
    {
        add{
            Events.AddHandler(EventCommand,value);
        }
        remove
        {
            Events.RemoveHandler(EventCommand, value);
        }
    }

    protected virtual void OnCommand(CommandEventArgs args)
    {
        CommandEventHandler ceh = Events[EventCommand] as CommandEventHandler;
        if (ceh != null)
            ceh(this, args);
        base.RaiseBubbleEvent(this,args);
    }
#endregion

    #region "Properties"
    public string CommandName
    {
        get
        {
            string s = ViewState["CommandName"] as string;
            if (s != null)
                return s;
            return String.Empty;
           
        }
        set
        {
            ViewState["CommandName"] = value;
        }
    }

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

    #endregion

    #region "Raising the event"

    protected override void OnCheckedChanged(EventArgs e)
    {
        base.OnCheckedChanged(e);

        string strChecked = this.Checked ? "true" : "false";
        if(CommandArgument != String.Empty)
            strChecked += ";" + CommandArgument;

        OnCommand(new CommandEventArgs(this.CommandName,strChecked));
    }

    #endregion
}

Then using this on your ITemplate and page (for example)


//The template
public class MyCheckBoxTemplate : ITemplate
{
    void ITemplate.InstantiateIn(Control parent)
    {
        CommandCheckBox cb = new CommandCheckBox();
        cb.CommandName = "CBCheck";
        cb.ID = "CB1";
        cb.AutoPostBack = true;
        parent.Controls.Add(cb);
       
    }

}

//Usage example on aspx

<asp:Label ID="lblMessage" runat="server" /><br /><br />
    <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="false" OnRowCommand="grid1_RowCommand"  >
       
    </asp:GridView>

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        TemplateField field = new TemplateField();
        field.HeaderText = "SampleBox";
        grid1.Columns.Add(field);
        field.ItemTemplate = new MyCheckBoxTemplate();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if(!IsPostBack)
        {
            //Just to create three rows with databinding
            ArrayList dummyData = new ArrayList();
            dummyData.Add(1);
            dummyData.Add(2);
            dummyData.Add(3);
            grid1.DataSource = dummyData;
            grid1.DataBind();

        }
    }
    protected void grid1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "CBCheck")
        {
            Control source = e.CommandSource as Control;
            GridViewRow row = source.NamingContainer as GridViewRow;
            lblMessage.Text = "Row " + (row.RowIndex + 1).ToString() + "=" + e.CommandArgument;
        }
    }

 

Category: GridView | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 9508 | Hits: 139

Similar Posts

  • The Telerik CAB Enabling Kit and SCSF - Tutorial 5: The RadPanelBar UIExtensionSite more
  • Multiple child views with RadGridView for WinForms more
  • Web.UI ASP.NET Grid: Synchronize Checkbox States with Row Selection more
  • Using ASP.NET 3.5's ListView and DataPager Controls: Editing Data more
  • WPF Release History : Q1 2009 SP2 (version 2009.1.526) more
  • Silverlight Release History : Q1 2009 SP2 (version 2009.1.526) more
  • Silverlight Release History : Q1 2009 SP1 (version 2009.1.413) more
  • WPF Release History : Q1 2009 SP1 (version 2009.1.413) more
  • Silverlight Release History : Q1 2009 (version 2009.1.312) more
  • Get SOLID: Single Responsibility Principle more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD