Help with reading Dynamically Added Controls

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

  1. I someone can please help, I am about at an end in trying to figure this out.
    I am adding some dynamic controls to my page (I found out that I was supposed
    to be doing that in the oninit event, which I am). I now want to read the
    text/values of those controls. I have found out that I can read the values if
    I wait until Page_Load, but then I have the same questions showing up again
    (along with the new questions) and I do not want them to. Can someone please
    help?

    Here is what I want to do:
    1. I want to be able to load up a screen of questions (dynamically from a
    database).
    2. Have the user answer them.
    3. When the user submits the screen they are on, save the answers they just
    completed and load up the next set of dynamic questions.

    Note: The code below is a mess as I have been trying different things.




    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace TDCS.Surveys
    {
    /// <summary>
    /// Summary description for AddUpdateAnswersTakeSurvey.
    /// </summary>
    public class AddUpdateAnswersTakeSurvey : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnBegin;
    protected System.Web.UI.WebControls.Button btnSubmitAnswers;
    protected System.Web.UI.WebControls.PlaceHolder phBuildingBlocks;
    protected System.Web.UI.WebControls.Panel pnlBuildingBlocks;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    //Load the instructions and make sure the user selected is assigned to
    take the selected survey.
    Label label = new Label();
    label.Text = Survey.GetSurveyInstructions(Session["SurveyID"].ToString());
    phBuildingBlocks.Controls.Add(label);
    Survey.CheckSurveyAssignment(Session["SurveyID"].ToString(),
    Session["PersonnelID"].ToString(), null);
    }
    else
    SaveAnswers();
    }


    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();

    //Only run this on the page being posted back.
    if (Page.IsPostBack)
    {
    LoadPreviousScreen();
    LoadSurveyPage();
    }

    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.btnBegin.Click += new System.EventHandler(this.btnBegin_Click);
    this.btnSubmitAnswers.Click += new
    System.EventHandler(this.btnSubmitAnswers_Click);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void btnBegin_Click(object sender, System.EventArgs e)
    {
    btnBegin.Visible = false;
    btnSubmitAnswers.Visible = true;
    }


    private void LoadPreviousScreen()
    {
    //If the Session is null then there is no previous screen yet.
    if (Session["ScreenNumber"] != null)
    {
    DataTable mainQuestions = Question.List(Session["SurveyID"].ToString(),
    int.Parse(Session["ScreenNumber"].ToString()) - 1);

    for(int index = 0; index <= mainQuestions.Rows.Count - 1; index++)
    {
    Label label = new Label();
    label.Font.Size = 11;
    label.Text = "<p>" + mainQuestions.Rows[index]["QuestionIdentifier"] +
    ". " + mainQuestions.Rows[index]["QuestionText"] + "<br><br>";
    label.CssClass = "formlabel";
    phBuildingBlocks.Controls.Add(label);

    //Get the building blocks that belong to the current question.
    Question questions =
    Question.GetBuildingBlocks(mainQuestions.Rows[index]["QuestionID"].ToString());

    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 LoadSurveyPage()
    {
    int screenNumber = 1;

    //If the Session is null then this is being loaded for the first time. Set
    //the Session and use it on successive loads.
    if (Session["ScreenNumber"] != null)
    screenNumber = int.Parse(Session["ScreenNumber"].ToString());
    else
    //Session is not null, so increment it so we know what page to display
    next.
    Session.Add("ScreenNumber", screenNumber + 1);

    DataTable mainQuestions = Question.List(Session["SurveyID"].ToString(),
    screenNumber);

    //Check to see if question count = 0. If so, the user has completed the
    //survey. Send them back to AddUpdateAnswers.
    if (mainQuestions.Rows.Count == 0)
    {
    Server.Transfer("AddUpdateAnswers.aspx");
    }
    else
    {
    for(int index = 0; index <= mainQuestions.Rows.Count - 1; index++)
    {
    Label label = new Label();
    label.Font.Size = 11;
    label.Text = "<p>" + mainQuestions.Rows[index]["QuestionIdentifier"] +
    ". " + mainQuestions.Rows[index]["QuestionText"] + "<br><br>";
    label.CssClass = "formlabel";
    phBuildingBlocks.Controls.Add(label);

    //Get the building blocks that belong to the current question.
    Question questions =
    Question.GetBuildingBlocks(mainQuestions.Rows[index]["QuestionID"].ToString());

    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 btnSubmitAnswers_Click(object sender, System.EventArgs e)
    {
    //We do not need any code here because all the functionality is in the
    initialize event.
    }


    private void SaveAnswers()
    {
    ArrayList dynamicControls = new ArrayList();
    IterateThroughChildren(phBuildingBlocks);
    }


    private void IterateThroughChildren(Control parent)
    {
    foreach(Control c in phBuildingBlocks.Controls)
    {
    switch (c.GetType().ToString())
    {
    case "System.Web.UI.WebControls.TextBox":
    TextBox textBox = ((TextBox)Page.FindControl(c.ID));
    Response.Write(textBox.Text);
    break;

    case "System.Web.UI.WebControls.DropDownList":
    DropDownList dropDownList = ((DropDownList)Page.FindControl(c.ID));
    break;

    case "System.Web.UI.WebControls.RadiobuttonList":
    RadioButtonList radioButtonList =
    ((RadioButtonList)Page.FindControl(c.ID));
    break;

    case "System.Web.UI.WebControls.CheckBoxList":
    CheckBoxList checkBoxList = ((CheckBoxList)Page.FindControl(c.ID));

    if (checkBoxList.Items[0].Selected)
    Response.Write("selected");

    break;
    }
    }
    }


    }
    }
     
    =?Utf-8?B?TWlrZSBDb2xsaW5z?=, Aug 7, 2006
    #1
    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. Jeffrey Todd
    Replies:
    1
    Views:
    4,174
    Peter Blum
    Jun 2, 2005
  2. Dennis Fazekas
    Replies:
    5
    Views:
    4,933
    =?Utf-8?B?RGVubmlzIEZhemVrYXM=?=
    Jan 1, 2006
  3. David Hubbard
    Replies:
    2
    Views:
    645
    David Hubbard
    Jan 17, 2006
  4. =?Utf-8?B?TWlrZSBDb2xsaW5z?=

    Help with reading Dynamically Added Controls

    =?Utf-8?B?TWlrZSBDb2xsaW5z?=, Aug 7, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    811
    =?Utf-8?B?TWlrZSBDb2xsaW5z?=
    Aug 7, 2006
  5. ENathan

    existing controls overlaying dynamically added controls

    ENathan, Jan 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    218
    Steve C. Orr [MVP, MCSD]
    Jan 10, 2005
Loading...

Share This Page