We have a diamically created radgrid with lots checkbox column in it. They all display fine. But I have problem for looping through it and get it's value. It seems can't find the checkbox control on the page. Not sure where I missed.
foreach (Functions function in CurrentUserFunctionList)
{
// Set up the RadGrid's columns
// NOTE: We have to use a custom TemplateColumn, because each checkbox is its own unique control, with its own unique ID.
GridTemplateColumn gtcFunction = new GridTemplateColumn();
gtcFunction.HeaderText = function.Description;
gtcFunction.DataField = function.FunctionId.ToString();
gtcFunction.ReadOnly = false;
gtcFunction.AllowFiltering = false;
// NOTE: The FunctionId gets passed in because it's part of each checkbox control's ID...
gtcFunction.ItemTemplate = new RadGridCheckBoxTemplate(function.FunctionId.ToString());
rgWeb.MasterTableView.Columns.Add(gtcFunction);
// Set up the Data Table's columns
DataColumn thiscolumn = new DataColumn();
thiscolumn.Caption = function.Description;
thiscolumn.ColumnName = function.FunctionId.ToString();
thiscolumn.ReadOnly = false;
thiscolumn.DataType = System.Type.GetType("System.Boolean");
MyDataTable.Columns.Add(thiscolumn);
}
}
public class RadGridCheckBoxTemplate : System.Web.UI.ITemplate
{
private string colname;
/// <
summary
>
/// This class sets up a Template with dynamically named CheckBoxes in the RadGrid. cName is the unique identifier for th column,
/// which is added to the ID of each CheckBox control.
/// </
summary
>
/// <
param
name
=
"type"
></
param
>
public RadGridCheckBoxTemplate(string cName)
{
this.colname = cName;
}
/// <
summary
>
/// Create the template
/// </
summary
>
/// <
param
name
=
"container"
></
param
>
public void InstantiateIn(System.Web.UI.Control container)
{
CheckBox checkbox = new CheckBox();
//checkbox.AutoPostBack = true;
checkbox.DataBinding += new EventHandler(boolValue_DataBinding);
checkbox.ID = "cbox_" + colname;
checkbox.CheckedChanged += new EventHandler(ToggleRowSelection);
container.Controls.Add(checkbox);
}
void boolValue_DataBinding(object sender, EventArgs e)
{
CheckBox cBox = (CheckBox)sender;
GridDataItem container = (GridDataItem)cBox.NamingContainer;
cBox.Checked = (bool)((DataRowView)container.DataItem)[colname];
}
/// <
summary
>
/// Check and uncheck
/// </
summary
>
/// <
param
name
=
"sender"
></
param
>
/// <
param
name
=
"e"
></
param
>
void ToggleRowSelection(object sender, EventArgs e)
{
((sender as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked;
}
}
use CheckBox cb = (CheckBox)gdi.FindControl(controlname);
or :
foreach (Control ctrl in Page.Controls)
{
if (ctrl is CheckBox)
{
string a = ctrl.ID;// never comes here to show the control id
}
}
I can't find any chekbox control on my page, not sure why?