1

I want to add runat=server dynamically to a CheckBoxList so that it can be found by FindControl.

CheckBoxList cbl = new CheckBoxList();
cbl.ID = "cbl" + intQuestionCount.ToString();

// get choices from choice list
int intChoiceListId = Convert.ToInt32(detail.ChoiceListID);
var choiceList = (from cl in _svsCentralDataContext.SVSSurvey_ChoiceListItems
                  where cl.ChoiceListID == intChoiceListId
                  orderby cl.Description
                  select cl);
cbl.DataSource = choiceList;
cbl.DataTextField = "Description";
cbl.DataBind();
cbl.Visible = true;
cbl.CssClass = "PositionCol3";

questionsPanel.Controls.Add(cbl);

I have 2 recursive find control methods:

    private HtmlControl FindHtmlControlByIdInControl(Control control, string id)
    {
        foreach (Control childControl in control.Controls)
        {
            if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase) 
                && childControl is HtmlControl
                )
            {
                return (HtmlControl)childControl;
            }

            if (childControl.HasControls())
            {
                HtmlControl result = FindHtmlControlByIdInControl(childControl, id);
                if (result != null)
                {
                    return result;
                }
            }
        }

        return null;
    }

    private WebControl FindWebControlByIdInControl(Control control, string id)
    {
        foreach (Control childControl in control.Controls)
        {
            if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase)
                && childControl is WebControl
                )
            {
                return (WebControl)childControl;
            }

            if (childControl.HasControls())
            {
                WebControl result = FindWebControlByIdInControl(childControl, id);
                if (result != null)
                {
                    return result;
                }
            }
        }

        return null;
    }

The screen is initially created dynamically (if !isPostback), based on an SQL record. The FindControl methods are used after this lot has been displayed, when the user clicks the 'Save' button. Neither Find control method finds my CheckBoxList!!

Steve Staple
  • 2,983
  • 9
  • 38
  • 73
  • 1
    What you have is a server-side control already, you should be able to find it with FindControl as it is. – Andrei Feb 24 '14 at 15:57
  • FindControl isn't recursive, that could be the issue you're having: _This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls_ http://msdn.microsoft.com/en-us/library/486wc64h%28v=vs.110%29.aspx – BlackICE Feb 24 '14 at 16:01
  • What event are you in when you create this control and add it to the controls group? At a guess, you're doing it too late in the page life cycle. – ThatBlairGuy Feb 24 '14 at 16:01
  • Would the CheckBoxList class as a WebControl or an HtmlControl? – Steve Staple Feb 24 '14 at 16:04
  • The Controls are created first time Page Loads. I am attempting to read them back in, along with the users responses, after a button is clicked. – Steve Staple Feb 24 '14 at 16:42

1 Answers1

4

You are adding controls through your code behind, they are already server side controls, you don't have to add runat="server". You are not finding them properly.

Make sure they are added to the page before you look for them.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • The controls have been definitely added to the page, because they are displayed. When save is clicked - they don't appear to be there. – Steve Staple Feb 25 '14 at 09:28
  • @SteveStaple, When ever you do a button click it will cause a `PostBack` and since web is stateless, those controls would be lost. You have to keep their state. You may see [this question](http://stackoverflow.com/questions/17589268/dynamically-created-controls-losing-data-after-postback) for further details and also [this answer](http://stackoverflow.com/a/4218690/961113) – Habib Feb 25 '14 at 14:08
  • I have finally got it to work. I had to store details of all my dynamic controls in session variables, so I could recreate them OnInit. The system miraculously manages to remember the values entered in the fields by the user, even if it does not remember the fields. – Steve Staple Feb 25 '14 at 14:15
  • @SteveStaple, yes that could be one way to do it, just be cautious about storing in session, since sessions are maintained for each user on server. You can also look into [ViewState](http://msdn.microsoft.com/en-us/library/ms972976.aspx) which is per page. – Habib Feb 25 '14 at 14:21
  • ViewState was no good - it would not store Textboxes, Labels, Checkboxes, CheckBoxLists or radioButtonList, as they are not 'serialized', whatever that means. – Steve Staple Feb 25 '14 at 15:14