Solved

isPostBack not working correctly

Posted on 2009-07-11
3
839 Views
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++)
             {


                 Response.Write(al[j].ToString());
             }

         }

         else
         {
         */

            QuesDSTableAdapters.QuestionTableAdapter questionsAdapter = new
     QuesDSTableAdapters.QuestionTableAdapter();
            QuesDS.QuestionDataTable questions;

            questions = questionsAdapter.GetQuestions();



            AnsDSTableAdapters.QueAnsTableAdapter queansAdapter = new
     AnsDSTableAdapters.QueAnsTableAdapter();

            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;
                Form.Controls.Add(ddl);

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

                ddl.DataSource = answer;

                // DropDownList1.Items.Add(Convert.ToString(answer.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(btnSubmitResults);


        }


    }










    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)ctl).SelectedValue.ToString());


                }

            }

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


                Response.Write(alist[j].ToString());
            }


        }
    }
0
Comment
Question by:ee-itpro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Expert Comment

by:Joep_Killaars
ID: 24831162
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";
 
            this.Form.Controls.Add(_ddl);
 
            if (!IsPostBack)
            {
                _ddl.Items.Add("A");
                _ddl.Items.Add("B");
                _ddl.Items.Add("C");
            }
            else
            {
                foreach (Control ctrl in this.Form.Controls)
                    if (ctrl.ID == "DropDownListId1")
                    {
                        _ddl = (DropDownList)ctrl;
                        break;
                    }
            }
 
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(_ddl.SelectedValue);
 
            _ddl.Visible = false;
        }

Open in new window

0
 
LVL 3

Accepted Solution

by:
Joep_Killaars earned 500 total points
ID: 24831169
the else clause is not necessary, the correct snippet :
public partial class WebForm1 : System.Web.UI.Page
    {
        private DropDownList _ddl;
 
        protected void Page_Init()
        {
            _ddl = new DropDownList();
            _ddl.ID = "DropDownListId1";
 
            this.Form.Controls.Add(_ddl);
 
            if (!IsPostBack)
            {
                _ddl.Items.Add("A");
                _ddl.Items.Add("B");
                _ddl.Items.Add("C");
            }
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(_ddl.SelectedValue);
 
            _ddl.Visible = false;
        }
    }

Open in new window

0
 

Author Closing Comment

by:ee-itpro
ID: 31602438
Hi,

  Thanks, that was helpful.  
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question