This is a migrated thread and some comments may be shown as answers.

Adding CheckBox columns programmatically with Events

1 Answer 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan Harvey
Top achievements
Rank 2
Dan Harvey asked on 28 Mar 2012, 04:00 PM
Hello,

I am having trouble adding "OnChecked" events to my GridTemplateColumns I am creating programmatically.

Here is the code I am using to create my columns (and the new column "RoleColumn"):
               public void DefineGridStructure()
            {
                var templateColumn = new GridTemplateColumn();
                templateColumn.UniqueName = "myColumn" + i;
                templateColumn.ItemTemplate = new RoleColumn("myColumn" + i);
  
                 // add the dynamically created columns to the grid
                dgLoginRequest.MasterTableView.Columns.AddAt(COLUMN_START_POS - 1, templateColumn);
            }
  
  
    public class RoleColumn : ITemplate
    {
  
        protected CheckBox roleCheckbox;
        private string _columnName;
  
        public RoleColumn(string columnName)
        {
            _columnName = columnName;
        }
  
        public void InstantiateIn(Control container)
        {
            roleCheckbox = new CheckBox { ID = "cbx" + _columnName };
            roleCheckbox.AutoPostBack = true;
            roleCheckbox.DataBinding += new EventHandler(roleCheckbox_DataBinding);
            container.Controls.Add(roleCheckbox);
        }
  
        private void roleCheckbox_DataBinding(object sender, EventArgs e)
        {
            var cBox = (CheckBox)sender;
            var container = (GridDataItem)cBox.NamingContainer;
            cBox.Checked = ((EntityUserRole)container.DataItem).Roles[0][_columnName];
        }
  
    }


Now when I call the DefineGridStructure routine I then bind a data source to my grid (the datasource is an ArrayList type which holds "Role" objects):

           public void BindDataGrid()
        {
            try
            {
                DefineGridStructure();
                dgLoginRequest.DataSource = DataSource;
                dgLoginRequest.DataBind();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

    public

 

 

interface IRole

 

    {

 

 

        Int16 RoleID { get; set; }

 

 

 

        string RoleName { get; set; }

 

 

 

        Int16 OrderID { get; set; }

 

    }



I then capture the OnItemDataBoundEvent and add my event handler at this point;
            protected void dgLoginRequest_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            

                    var i = 0;
                foreach (Role role in Roles)
                {
                    var checkbox = (e.Item as GridDataItem)["myColumn" + i].Controls[0] as CheckBox;
                    checkbox.CheckedChanged += new EventHandler(cbxReader_CheckedChanged); 
                            i++;
                }
                  
            }
        }

However, when I click my checkboxes they do not fire the event (cbx_CheckedChanged).

What am I missing?

All I want to do is create a checkbox column for each role that I have in my Arraylist (currently 4).  And I want to be able to handle the "OnCheckedChanged" event in code whenever any of these checkboxes are checked.

Please help.  I have went through the documenation already - that's how i got this far.  A sample application would be appreciated.

Thanks,

1 Answer, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 02 Apr 2012, 08:57 AM
Hello Dan,

You could resolve your issue by setting the CheckBox control AutoPostBack property to true in order to successfully call the server-side CheckedChanged event as it is shown below.
foreach (Role role in Roles)
{
    var checkbox = (e.Item as GridDataItem)["myColumn" + i].Controls[0] as CheckBox;
    checkbox.CheckedChanged += new EventHandler(cbxReader_CheckedChanged);
    checkbox.AutoPostBack = true;
    i++;
}

All the best,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Dan Harvey
Top achievements
Rank 2
Answers by
Antonio Stoilkov
Telerik team
Share this question
or