472,808 Members | 3,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,808 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 2332

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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.