473,385 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with reading Dynamically Added Controls

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;
}
}
}
}
}

Aug 7 '06 #1
0 2376

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
5
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
3
by: Dotnet Gruven | last post by:
I've built a WebForm with a Table added dynamically in Page_Load when IsPostBack is false. The table includes a couple of TextBoxes, RadioButtonLists and CheckboxLists. On postback, those...
1
by: Mike Collins | last post by:
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,...
5
by: Chris | last post by:
I have a page with mixture of static and dynamically added controls is there any way of controlling the order which they are added to the page. My submit button (statically added) appears before...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.