We help IT Professionals succeed at work.

isPostBack not working correctly

ee-itpro asked
Medium Priority
Last Modified: 2012-05-07
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++)




            QuesDSTableAdapters.QuestionTableAdapter questionsAdapter = new
            QuesDS.QuestionDataTable questions;

            questions = questionsAdapter.GetQuestions();

            AnsDSTableAdapters.QueAnsTableAdapter queansAdapter = new

            AnsDS.QueAnsDataTable answer;

            // AnsDS.QueAnsDataTable queans;

            foreach (QuesDS.QuestionRow quesRow in questions)

                //Response.Write("Question: " + quesRow.Que + "<br />");

                //answer = queansAdapter.GetAnswersByQuestionId(quesRow.QID);

                Form.Controls.Add(new LiteralControl(quesRow.Que));

                answer = queansAdapter.GetAnswersByQuestionId(quesRow.QID);

                DropDownList ddl = new DropDownList();

                ddl.ID = "ddl" + Convert.ToString(i);
                Form.Controls.Add(new LiteralControl("<br>"));

                i += 1;

                // ddl.DataSource = queansAdapter.GetAnswersByQuestionId(Convert.ToInt32(quesRow.QID));

                ddl.DataSource = answer;

                // DropDownList1.Items.Add(Convert.ToString(answer.Columns ["Answer"]));
                ddl.DataTextField = "Answer";

                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;



    protected void SubmitResults(object sender, EventArgs e)
        ArrayList alist = new ArrayList();

        int j;

            foreach (Control ctl in form1.Controls)
                if ((ctl) is DropDownList)



            for (j = 0; j <= 2; j++)


Watch Question

Look at the attached code snippet.

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.
private DropDownList _ddl;
        protected void Page_Init()
            _ddl = new DropDownList();
            _ddl.ID = "DropDownListId1";
            if (!IsPostBack)
                foreach (Control ctrl in this.Form.Controls)
                    if (ctrl.ID == "DropDownListId1")
                        _ddl = (DropDownList)ctrl;
        protected void Button1_Click(object sender, EventArgs e)
            _ddl.Visible = false;

Open in new window

Unlock this solution and get a sample of our free trial.
(No credit card required)



  Thanks, that was helpful.  
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.