Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databound DropDownList + ViewState

Status
Not open for further replies.

RhythmAddict112

Programmer
Joined
Jun 17, 2004
Messages
625
Location
US
Hi All,
I realize this may be worthless without code to follow - so I will post the code when I get back to work tomorrow AM. In the mean time, I want to see if someone can help me.

I've got a c# page with a databound drop down list control. The DDL is created programatically, autopostback and enableViewState are both set to true. The DDL is within a placeHolder control; which also has enableViewState = true. I only query the DB + DataBind() if (!Page.IsPostBack) However, when it does post back, the DDL is empty...There are no values. However, if I drag a drop down list control onto my page with the designer (ie, dont create it programmatically) everything works fine. That is, I only build the DDL when it isn't PostBack, however the DDL is readily populated. What's the work around for this? Is there a better way to go about it? Thanks for your help in advance.

All hail the INTERWEB!
 
As promised...this all works perfectly if i dont programmatically generate my drop down list..

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Dundas.Charting.WebControl;
namespace CAMIT
{
	/// <summary>
	/// Summary description for ViewStateTest.
	/// </summary>
	public class ViewStateTest : System.Web.UI.Page
	{
		//protected System.Web.UI.WebControls.DropDownList DropDownList1;
		protected System.Web.UI.WebControls.DropDownList DropDownList1 = new DropDownList();
		public string Db2Conn = "Some Connection String";
		
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
		public string coreSQL = "select sum(actual) Actual, department, sum(Capacity) Capacity from  (select sum(pmain.estimate) Actual, loe.loe Capacity, deptgrp.description department from prtadmin.projectmain pmain, prtadmin.projectapp papp,prtadmin.application app, prtadmin.applicationgrp appgrp, prtadmin.departmentgrp deptgrp, prtadmin.applicationloe loe where pmain.projectid = papp.projectid and papp.applicationcd = app.applicationcd and appgrp.groupcd = app.applicationgrpcd and appgrp.deptgroupcd = deptgrp.deptgroupcd and app.applicationcd = loe.applicationcd and loe.year = 2006  and loe.month1 = 1 "; // ") group by deptgrp.description, loe.loe)  x group by department"
		public string groupSQL = " group by deptgrp.description, loe.loe)  x group by department";

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(Page.IsPostBack)
			{
				string execSQL = coreSQL;
				if(DropDownList1.SelectedValue != "0")
				{
					
					execSQL = coreSQL + " and papp.AppstatusCd= '" + DropDownList1.SelectedValue.ToString() + "' ";
				}

				Label1.Text = execSQL;


			}
			else
			{
				buildStatusDDL();

			}
		}
		void buildStatusDDL()
		{
	 
		/**Lets try programmatically creating the DDL
		 * DropDownList DropDownList1 = new DropDownList();
		 * **/
			DropDownList1.ID = "DropDownList1";
			DropDownList1.Visible = true;
			DropDownList1.AutoPostBack = true;
			DropDownList1.EnableViewState = true;
			ListItem ListItem0 = new ListItem();
			ListItem0.Text = "None";
			ListItem0.Value = "0";
			DropDownList1.Items.Add(ListItem0);
			string mySelectQuery="select statusgroupcd, description from prtadmin.statusgroup";
			OleDbConnection myConnection = new OleDbConnection(Db2Conn);
			myConnection.Open();
			OleDbCommand cmd = new OleDbCommand(mySelectQuery,myConnection);
			OleDbDataReader rdr = cmd.ExecuteReader();
			while(rdr.Read())
			{
				ListItem newListItem = new ListItem(); 
				newListItem.Text = rdr.GetString(1); 
				newListItem.Value = rdr.GetString(0); 
				DropDownList1.Items.Add(newListItem); 
			}
			 
			DropDownList1.DataBind();
			//Page.Controls.Add(DropDownList1);
			PlaceHolder1.Controls.Add(DropDownList1);
		}

		
		DataView getData(string pSQL)
		{

			OleDbConnection myConnection = new OleDbConnection(Db2Conn);
			Label1.Text = pSQL + groupSQL;
			OleDbDataAdapter cmd = new OleDbDataAdapter(pSQL + groupSQL,Db2Conn);
			System.Data.DataSet ds = new System.Data.DataSet();
			cmd.Fill(ds);
			DataView source = new DataView(ds.Tables[0]);
			return source;
		}
		
		void buildActualVCapacityChart(string pSQL)
		{
			//code		 
	 
		}

		#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();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		 void InitializeComponent()
		{    
			this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}

All hail the INTERWEB!
 
If you are creating dynamic controls, then you will have to create and rebind them regardless of whether it is a postback.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ah. Alright - thanks for our help!

All hail the INTERWEB!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top