ee-itpro
asked on
isPostBack not working correctly
In the following code, i generate dropdownlists dynamically and then try to add the selected list values to an arraylist and display them on a button(submit) click event.
I have a if(page.isPostBack == false) check inside the page_load event that checks if the page is being loaded for the first time and if it is it generates the lists dynamically and fills option values from database tables(through tableadapter methods).
Otherwise, if the button(submit) was clicked(that is if this was a postback), the selected values from the list should get added to the arraylist and then be displayed.
But this is not happening. Instead it shows some == output and no arraylist values display.
When i tried it without ispostback, it works fine(that is arraylist values get displayed) but then the form (dropdownlists themselves with all option values) are also displayed along with the output(arraylist values).
How to correct this behaviour so that it just displays the selected list values and not the form itself along with it?
Code Follows :
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls ;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls. WebParts;
using System.Xml.Linq;
using QuesDSTableAdapters;
using AnsDSTableAdapters;
using System;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
int i = 1;
//int j;
//ArrayList alist = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
/* for (j = 0; j<= 1; j++)
{
Response.Write(al[j].ToStr ing());
}
}
else
{
*/
QuesDSTableAdapters.Questi onTableAda pter questionsAdapter = new
QuesDSTableAdapters.Questi onTableAda pter();
QuesDS.QuestionDataTable questions;
questions = questionsAdapter.GetQuesti ons();
AnsDSTableAdapters.QueAnsT ableAdapte r queansAdapter = new
AnsDSTableAdapters.QueAnsT ableAdapte r();
AnsDS.QueAnsDataTable answer;
// AnsDS.QueAnsDataTable queans;
foreach (QuesDS.QuestionRow quesRow in questions)
{
//Response.Write("Question : " + quesRow.Que + "<br />");
//answer = queansAdapter.GetAnswersBy QuestionId (quesRow.Q ID);
Form.Controls.Add(new LiteralControl(quesRow.Que ));
answer = queansAdapter.GetAnswersBy QuestionId (quesRow.Q ID);
DropDownList ddl = new DropDownList();
ddl.ID = "ddl" + Convert.ToString(i);
Form.Controls.Add(new LiteralControl("<br>"));
i += 1;
Form.Controls.Add(ddl);
// ddl.DataSource = queansAdapter.GetAnswersBy QuestionId (Convert.T oInt32(que sRow.QID)) ;
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co nvert.ToSt ring(answe r.Columns ["Answer"]));
ddl.DataTextField = "Answer";
ddl.DataBind();
Form.Controls.Add(new LiteralControl("<br>"));
Form.Controls.Add(new LiteralControl("<br>"));
}
Button btnSubmitResults = new Button();
btnSubmitResults.ID = "btnSubmitResults";
btnSubmitResults.Text = "Submit Results";
btnSubmitResults.Click += SubmitResults;
Form.Controls.Add(btnSubmi tResults);
}
}
protected void SubmitResults(object sender, EventArgs e)
{
ArrayList alist = new ArrayList();
int j;
foreach (Control ctl in form1.Controls)
{
if ((ctl) is DropDownList)
{
alist.Add(((DropDownList)c tl).Select edValue.To String());
}
}
for (j = 0; j <= 2; j++)
{
Response.Write(alist[j].To String());
}
}
}
I have a if(page.isPostBack == false) check inside the page_load event that checks if the page is being loaded for the first time and if it is it generates the lists dynamically and fills option values from database tables(through tableadapter methods).
Otherwise, if the button(submit) was clicked(that is if this was a postback), the selected values from the list should get added to the arraylist and then be displayed.
But this is not happening. Instead it shows some == output and no arraylist values display.
When i tried it without ispostback, it works fine(that is arraylist values get displayed) but then the form (dropdownlists themselves with all option values) are also displayed along with the output(arraylist values).
How to correct this behaviour so that it just displays the selected list values and not the form itself along with it?
Code Follows :
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Xml.Linq;
using QuesDSTableAdapters;
using AnsDSTableAdapters;
using System;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
int i = 1;
//int j;
//ArrayList alist = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
/* for (j = 0; j<= 1; j++)
{
Response.Write(al[j].ToStr
}
}
else
{
*/
QuesDSTableAdapters.Questi
QuesDSTableAdapters.Questi
QuesDS.QuestionDataTable questions;
questions = questionsAdapter.GetQuesti
AnsDSTableAdapters.QueAnsT
AnsDSTableAdapters.QueAnsT
AnsDS.QueAnsDataTable answer;
// AnsDS.QueAnsDataTable queans;
foreach (QuesDS.QuestionRow quesRow in questions)
{
//Response.Write("Question
//answer = queansAdapter.GetAnswersBy
Form.Controls.Add(new LiteralControl(quesRow.Que
answer = queansAdapter.GetAnswersBy
DropDownList ddl = new DropDownList();
ddl.ID = "ddl" + Convert.ToString(i);
Form.Controls.Add(new LiteralControl("<br>"));
i += 1;
Form.Controls.Add(ddl);
// ddl.DataSource = queansAdapter.GetAnswersBy
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co
ddl.DataTextField = "Answer";
ddl.DataBind();
Form.Controls.Add(new LiteralControl("<br>"));
Form.Controls.Add(new LiteralControl("<br>"));
}
Button btnSubmitResults = new Button();
btnSubmitResults.ID = "btnSubmitResults";
btnSubmitResults.Text = "Submit Results";
btnSubmitResults.Click += SubmitResults;
Form.Controls.Add(btnSubmi
}
}
protected void SubmitResults(object sender, EventArgs e)
{
ArrayList alist = new ArrayList();
int j;
foreach (Control ctl in form1.Controls)
{
if ((ctl) is DropDownList)
{
alist.Add(((DropDownList)c
}
}
for (j = 0; j <= 2; j++)
{
Response.Write(alist[j].To
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks, that was helpful.
Thanks, that was helpful.
You should use the page_init event to create the control. Save the control as a private member of your page and you can do whatever you want with it, even after postbacks. In this case, set visible to false or simply remove the object from the control collection of the form.
Open in new window