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

2 posts, 0 answers
  1. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 25 Jul 2011 Link to this post

    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?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jul 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top