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"):
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):
I then capture the OnItemDataBoundEvent and add my event handler at this point;
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,
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,