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

loop through diamically created checkbox column problem - can't find checkbox control on the page

1 Answer 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JJ
Top achievements
Rank 1
JJ asked on 25 Jul 2011, 04:48 PM

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;
        }
    }
I need to loop throug the radgrid to get all the column,

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?

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Jul 2011, 06:09 AM
Hello JJ,

You can try the following code snippet to access the CheckBox control.
C#:
public void InstantiateIn(System.Web.UI.Control container) 
{
          . . . .    . . . .     . . . .        . . . .
    box.CheckedChanged += new EventHandler(ToggleRowSelection);
}
   void ToggleRowSelection(object sender, EventArgs e)
   {
      ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
   }

Thanks,
Shinu.
Tags
Grid
Asked by
JJ
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or