Enumerate through Controls

Discussion in 'ASP .Net' started by =?Utf-8?B?TWlrZSBDb2xsaW5z?=, Aug 4, 2006.

  1. I have some controls that I am creating dynamically. After the user finishes
    entering their answers in these controls, I would like to iterate through
    these controls and get the answers out. I have tried a couple of ways, but do
    not see those controls there. Can someone please help? Below is snippett of
    code to show how I am adding the controls and "for now" how I am trying to
    read them back.

    foreach(Question.BuildingBlock buildingBlock in
    questions.QuestionBuildingBlocks)
    {
    Label bbLabel = new Label();
    bbLabel.Text = "<p>" + buildingBlock.Identifier + ". " +
    buildingBlock.BuildingBlockText + "<br><br>";
    phBuildingBlocks.Controls.Add(bbLabel);

    switch(buildingBlock.BuildingBlockType.ToString())
    {
    case "Radio":
    RadioButtonList radioButtonList = new RadioButtonList();
    radioButtonList.ID = "survey_question_" +
    buildingBlock.Identifier.Trim();
    radioButtonList.DataValueField = "AnswerOptionID";
    radioButtonList.DataTextField = "AnswerOption";
    radioButtonList.DataSource =
    Answer.ListOptions(buildingBlock.AnswerGroupID);
    radioButtonList.DataBind();
    phBuildingBlocks.Controls.Add(radioButtonList);
    break;

    case "DropDown":
    DropDownList dropDownList = new DropDownList();
    dropDownList.ID = "survey_question_" +
    buildingBlock.Identifier.Trim();
    dropDownList.DataValueField = "AnswerOptionID";
    dropDownList.DataTextField = "AnswerOption";
    dropDownList.DataSource =
    Answer.ListOptions(buildingBlock.AnswerGroupID);
    dropDownList.DataBind();
    phBuildingBlocks.Controls.Add(dropDownList);
    break;

    case "CheckBoxes":
    CheckBoxList checkBoxList = new CheckBoxList();
    checkBoxList.ID = "survey_question_" +
    buildingBlock.Identifier.Trim();
    checkBoxList.DataValueField = "AnswerOptionID";
    checkBoxList.DataTextField = "AnswerOption";
    checkBoxList.DataSource =
    Answer.ListOptions(buildingBlock.AnswerGroupID);
    checkBoxList.DataBind();
    phBuildingBlocks.Controls.Add(checkBoxList);
    break;

    default: //Text
    TextBox textBox = new TextBox();
    textBox.ID = "survey_question_" + buildingBlock.Identifier.Trim();
    textBox.MaxLength = 8000;
    textBox.Width = 600;
    textBox.TextMode = TextBoxMode.MultiLine;
    textBox.Rows = 5;
    phBuildingBlocks.Controls.Add(textBox);
    break;
    }
    }
    }
    }



    private void SaveAnswers()
    {
    IterateThroughChildren(this);
    }


    private void IterateThroughChildren(Control parent)
    {
    foreach (Control c in phBuildingBlocks.Controls)
    {
    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
    {
    Response.Write(c.ID);
    }

    if (c.Controls.Count > 0)
    {
    IterateThroughChildren(c);
    }
    }
    }
     
    =?Utf-8?B?TWlrZSBDb2xsaW5z?=, Aug 4, 2006
    #1
    1. Advertising

  2. Try recurring through the controls and adding them to a control collection
    first if they are the right type (sorry this is bad code and in VB):

    Sub MyRecursiveAdd(ByVal c as Control, ByRef col as Collection)
    Dim child as Control
    For Each child In c.Controls
    If typeof child is TextBox Then
    col.Add(child)
    ElseIf typeof child is DropDown Then
    col.Add(child)
    End If
    if child.HasControls() Then
    MyRecursiveAdd(child,col)
    End If
    Next
    End Sub

    After a call to this routine, loop through all the controls in the returned
    collection and process them how you show below.

    "Mike Collins" wrote:

    > I have some controls that I am creating dynamically. After the user finishes
    > entering their answers in these controls, I would like to iterate through
    > these controls and get the answers out. I have tried a couple of ways, but do
    > not see those controls there. Can someone please help? Below is snippett of
    > code to show how I am adding the controls and "for now" how I am trying to
    > read them back.
    >
    > foreach(Question.BuildingBlock buildingBlock in
    > questions.QuestionBuildingBlocks)
    > {
    > Label bbLabel = new Label();
    > bbLabel.Text = "<p>" + buildingBlock.Identifier + ". " +
    > buildingBlock.BuildingBlockText + "<br><br>";
    > phBuildingBlocks.Controls.Add(bbLabel);
    >
    > switch(buildingBlock.BuildingBlockType.ToString())
    > {
    > case "Radio":
    > RadioButtonList radioButtonList = new RadioButtonList();
    > radioButtonList.ID = "survey_question_" +
    > buildingBlock.Identifier.Trim();
    > radioButtonList.DataValueField = "AnswerOptionID";
    > radioButtonList.DataTextField = "AnswerOption";
    > radioButtonList.DataSource =
    > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > radioButtonList.DataBind();
    > phBuildingBlocks.Controls.Add(radioButtonList);
    > break;
    >
    > case "DropDown":
    > DropDownList dropDownList = new DropDownList();
    > dropDownList.ID = "survey_question_" +
    > buildingBlock.Identifier.Trim();
    > dropDownList.DataValueField = "AnswerOptionID";
    > dropDownList.DataTextField = "AnswerOption";
    > dropDownList.DataSource =
    > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > dropDownList.DataBind();
    > phBuildingBlocks.Controls.Add(dropDownList);
    > break;
    >
    > case "CheckBoxes":
    > CheckBoxList checkBoxList = new CheckBoxList();
    > checkBoxList.ID = "survey_question_" +
    > buildingBlock.Identifier.Trim();
    > checkBoxList.DataValueField = "AnswerOptionID";
    > checkBoxList.DataTextField = "AnswerOption";
    > checkBoxList.DataSource =
    > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > checkBoxList.DataBind();
    > phBuildingBlocks.Controls.Add(checkBoxList);
    > break;
    >
    > default: //Text
    > TextBox textBox = new TextBox();
    > textBox.ID = "survey_question_" + buildingBlock.Identifier.Trim();
    > textBox.MaxLength = 8000;
    > textBox.Width = 600;
    > textBox.TextMode = TextBoxMode.MultiLine;
    > textBox.Rows = 5;
    > phBuildingBlocks.Controls.Add(textBox);
    > break;
    > }
    > }
    > }
    > }
    >
    >
    >
    > private void SaveAnswers()
    > {
    > IterateThroughChildren(this);
    > }
    >
    >
    > private void IterateThroughChildren(Control parent)
    > {
    > foreach (Control c in phBuildingBlocks.Controls)
    > {
    > if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
    > {
    > Response.Write(c.ID);
    > }
    >
    > if (c.Controls.Count > 0)
    > {
    > IterateThroughChildren(c);
    > }
    > }
    > }
    >
     
    =?Utf-8?B?S2VpdGg=?=, Aug 4, 2006
    #2
    1. Advertising

  3. you need to add the controls on postback

    -- bruce (sqlwork.com)


    "Mike Collins" <> wrote in message
    news:...
    >I have some controls that I am creating dynamically. After the user
    >finishes
    > entering their answers in these controls, I would like to iterate through
    > these controls and get the answers out. I have tried a couple of ways, but
    > do
    > not see those controls there. Can someone please help? Below is snippett
    > of
    > code to show how I am adding the controls and "for now" how I am trying to
    > read them back.
    >
    > foreach(Question.BuildingBlock buildingBlock in
    > questions.QuestionBuildingBlocks)
    > {
    > Label bbLabel = new Label();
    > bbLabel.Text = "<p>" + buildingBlock.Identifier + ". " +
    > buildingBlock.BuildingBlockText + "<br><br>";
    > phBuildingBlocks.Controls.Add(bbLabel);
    >
    > switch(buildingBlock.BuildingBlockType.ToString())
    > {
    > case "Radio":
    > RadioButtonList radioButtonList = new RadioButtonList();
    > radioButtonList.ID = "survey_question_" +
    > buildingBlock.Identifier.Trim();
    > radioButtonList.DataValueField = "AnswerOptionID";
    > radioButtonList.DataTextField = "AnswerOption";
    > radioButtonList.DataSource =
    > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > radioButtonList.DataBind();
    > phBuildingBlocks.Controls.Add(radioButtonList);
    > break;
    >
    > case "DropDown":
    > DropDownList dropDownList = new DropDownList();
    > dropDownList.ID = "survey_question_" +
    > buildingBlock.Identifier.Trim();
    > dropDownList.DataValueField = "AnswerOptionID";
    > dropDownList.DataTextField = "AnswerOption";
    > dropDownList.DataSource =
    > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > dropDownList.DataBind();
    > phBuildingBlocks.Controls.Add(dropDownList);
    > break;
    >
    > case "CheckBoxes":
    > CheckBoxList checkBoxList = new CheckBoxList();
    > checkBoxList.ID = "survey_question_" +
    > buildingBlock.Identifier.Trim();
    > checkBoxList.DataValueField = "AnswerOptionID";
    > checkBoxList.DataTextField = "AnswerOption";
    > checkBoxList.DataSource =
    > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > checkBoxList.DataBind();
    > phBuildingBlocks.Controls.Add(checkBoxList);
    > break;
    >
    > default: //Text
    > TextBox textBox = new TextBox();
    > textBox.ID = "survey_question_" + buildingBlock.Identifier.Trim();
    > textBox.MaxLength = 8000;
    > textBox.Width = 600;
    > textBox.TextMode = TextBoxMode.MultiLine;
    > textBox.Rows = 5;
    > phBuildingBlocks.Controls.Add(textBox);
    > break;
    > }
    > }
    > }
    > }
    >
    >
    >
    > private void SaveAnswers()
    > {
    > IterateThroughChildren(this);
    > }
    >
    >
    > private void IterateThroughChildren(Control parent)
    > {
    > foreach (Control c in phBuildingBlocks.Controls)
    > {
    > if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
    > {
    > Response.Write(c.ID);
    > }
    >
    > if (c.Controls.Count > 0)
    > {
    > IterateThroughChildren(c);
    > }
    > }
    > }
    >
     
    bruce barker \(sqlwork.com\), Aug 4, 2006
    #3
  4. I was thinking they were not persisting. So, I need to figure out how to make
    them persist so I can enumerate through them and then get the value that is
    in them on the postback?

    "bruce barker (sqlwork.com)" wrote:

    > you need to add the controls on postback
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Mike Collins" <> wrote in message
    > news:...
    > >I have some controls that I am creating dynamically. After the user
    > >finishes
    > > entering their answers in these controls, I would like to iterate through
    > > these controls and get the answers out. I have tried a couple of ways, but
    > > do
    > > not see those controls there. Can someone please help? Below is snippett
    > > of
    > > code to show how I am adding the controls and "for now" how I am trying to
    > > read them back.
    > >
    > > foreach(Question.BuildingBlock buildingBlock in
    > > questions.QuestionBuildingBlocks)
    > > {
    > > Label bbLabel = new Label();
    > > bbLabel.Text = "<p>" + buildingBlock.Identifier + ". " +
    > > buildingBlock.BuildingBlockText + "<br><br>";
    > > phBuildingBlocks.Controls.Add(bbLabel);
    > >
    > > switch(buildingBlock.BuildingBlockType.ToString())
    > > {
    > > case "Radio":
    > > RadioButtonList radioButtonList = new RadioButtonList();
    > > radioButtonList.ID = "survey_question_" +
    > > buildingBlock.Identifier.Trim();
    > > radioButtonList.DataValueField = "AnswerOptionID";
    > > radioButtonList.DataTextField = "AnswerOption";
    > > radioButtonList.DataSource =
    > > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > > radioButtonList.DataBind();
    > > phBuildingBlocks.Controls.Add(radioButtonList);
    > > break;
    > >
    > > case "DropDown":
    > > DropDownList dropDownList = new DropDownList();
    > > dropDownList.ID = "survey_question_" +
    > > buildingBlock.Identifier.Trim();
    > > dropDownList.DataValueField = "AnswerOptionID";
    > > dropDownList.DataTextField = "AnswerOption";
    > > dropDownList.DataSource =
    > > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > > dropDownList.DataBind();
    > > phBuildingBlocks.Controls.Add(dropDownList);
    > > break;
    > >
    > > case "CheckBoxes":
    > > CheckBoxList checkBoxList = new CheckBoxList();
    > > checkBoxList.ID = "survey_question_" +
    > > buildingBlock.Identifier.Trim();
    > > checkBoxList.DataValueField = "AnswerOptionID";
    > > checkBoxList.DataTextField = "AnswerOption";
    > > checkBoxList.DataSource =
    > > Answer.ListOptions(buildingBlock.AnswerGroupID);
    > > checkBoxList.DataBind();
    > > phBuildingBlocks.Controls.Add(checkBoxList);
    > > break;
    > >
    > > default: //Text
    > > TextBox textBox = new TextBox();
    > > textBox.ID = "survey_question_" + buildingBlock.Identifier.Trim();
    > > textBox.MaxLength = 8000;
    > > textBox.Width = 600;
    > > textBox.TextMode = TextBoxMode.MultiLine;
    > > textBox.Rows = 5;
    > > phBuildingBlocks.Controls.Add(textBox);
    > > break;
    > > }
    > > }
    > > }
    > > }
    > >
    > >
    > >
    > > private void SaveAnswers()
    > > {
    > > IterateThroughChildren(this);
    > > }
    > >
    > >
    > > private void IterateThroughChildren(Control parent)
    > > {
    > > foreach (Control c in phBuildingBlocks.Controls)
    > > {
    > > if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
    > > {
    > > Response.Write(c.ID);
    > > }
    > >
    > > if (c.Controls.Count > 0)
    > > {
    > > IterateThroughChildren(c);
    > > }
    > > }
    > > }
    > >

    >
    >
    >
     
    =?Utf-8?B?TWlrZSBDb2xsaW5z?=, Aug 4, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. poi

    Enumerate Roles?

    poi, Nov 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    520
    Oliver
    Nov 15, 2003
  2. shiv
    Replies:
    3
    Views:
    6,816
    S. Justin Gengo
    Dec 3, 2003
  3. localhost

    Enumerate Control Attributes?

    localhost, Dec 16, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    630
    localhost
    Dec 22, 2003
  4. The Colonel

    enumerate datagrid controls (VB)

    The Colonel, Jan 16, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    421
    The Colonel
    Jan 16, 2005
  5. J
    Replies:
    3
    Views:
    7,188
Loading...

Share This Page