ASP.NET Web Controls >> DropDownList items disappearing after 2 postbacks

by howcheng » Fri, 05 Sep 2003 02:55:52 GMT

I have a page with a big chart/table on it. Users can click the
LinkButton headers in the table to sort by the values in that column.
A few of the columns have DropDownLists in the headers for filtering
purposes (i.e., pick a value from the list and only rows with that
value in that column will appear). Make sense so far?

The sorting LinkButtons all postback to one CommandEventHandler. The
DropDownLists all AutoPostBack to a single EventHandler. The
CommandArguments of the LinkButtons are hard-coded into the ASPX file.
The DropDownLists are also hard-coded into the ASPX file, but their
ListItems are dynamically added during Page_Load if it's not a
postback (values come from an XML file). The values being displayed
all come out of the same XML document, are put into a DataSet, and
then sorted/filtered depending on the user's choices. The DataSet is
then bound to a Repeater.

The problem I'm experiencing is that on the first postback AFTER a
sort operation, the DropDownLists lose all of their ListItems. In
other words, if I do two sort operations in a row, they empty. If I do
a filtering operation (i.e., get one of the DropDownLists to
AutoPostBack) then a sort, followed by another filter, then they
disappear. If I do mulitple filtering operations only, nothing
disappears.

I've run this through the VS.NET debugger and after the second
postback, the DropDownLists' Items.Count property is zero. No
explanation as to why all the ListItems disappear.

I have a sample and source code posted at
http://howard.zentropy.com/ct09.aspx. If anyone could PLEASE take a
look and tell me if I'm doing anything wrong, or even that it's a
known ASP.NET bug, that would be greatly appreciated. I've been
tackling this thing for two days now and am going seriously bonkers
trying to figure it out.

Thanks in advance.

Similar Threads

1. DropDownList with Postback on only one item...

2. determining a dropdownlist selected item without doing a postback - ASP.NET Web Controls

3. DataGrid item dropdownlist javascript disable

Hello all,.
I have a datagrid and one of the columns has a static dropdownlist
which means each column has the exact same dropdownlist.  On the c#
side values are pulled from the database and depending on the value
from the db it should either keep the dropdownlist enabled or it should
set it as disabled for that row.  I believe some javascript like
document.getElementById would help somehow but I'm still learning.

Eg.
Each row has a color list of:

BROWN
BLUE
ORANGE

if the database says that the row value is null then disable & hide
that column for that row.  Meaning that item doesn't need a color.

If someone could help that would be great.  Thanks in advance.

4. Disable databound dropdownlist if 0 items - ASP.NET Web Controls

5. Disab;e selection of dropdownlist items (in a GridView)

Is there an example of using client side script to disable selection of some 
items in a drop down that I could follow.

It would seem tricky as there are multiple drop downs in the GridView.  I 
would need to remember the last selected value for each drop down within the 
grid.  If the selected value is not permitted then reset to the remembered 
value.

And then when a valid item is selected the remebered value for that drop 
down would need to be updated.

Are there any links or examples that I could utilise?

6. DropDownList Disabled Items in the list - Asp.Net

7. DropDownList 2 always returns Selected = 0 for all items - even selected item

Hi All

I have 2 DropDownList boxes on a page.

The first (id= "Operation") is populated on PageLoad with the contents
of a database table.
The second id="WorkStations" will not be populated until the first has
been changed
The definitions are below.

<ASP:DropDownList id="Operation"
      runat="server"
      AppendDataBoundItems="True"
      AutoPostBack="True"
     OnSelectedIndexChanged="Operation_SelectedIndexChanged">
</ASP:DropDownList>  

<ASP:DropDownList id="WorkStations"
      runat="server"
      AppendDataBoundItems="True"
      AutoPostBack="True"
      OnSelectedIndexChanged="WorkStations_SelectedIndexChanged">
</ASP:DropDownList>  
.
Both events fire successfully - on every item change.

When I change the Operation box it fires correctly and the WorkStations
box is populated correctly. However when I select an item in the
WorkStations box none of the items are listed as Selected in the event
handler. The event fires - I am using Delphi Developer Studio 2006 to
develop and I have debugged the event handler - and the dropdownlist
box is being picked up.

The 2 event handlers are

protected void Operation_SelectedIndexChanged(object sender, EventArgs
e)
  {
  OleDbConnection conoper = new OleDbConnection("provider=IBMDA400;Data
Source=xx.xxx.xx.xxx;User Id=xxxxxxx;password=xxxxxxxxxx");
  OleDbCommand cmdoper = new OleDbCommand();
  string LString;

  foreach(ListItem item in Operation.Items)  // This is dropdownlist
box 1
    {
	if(item.Selected)
	{
	if (item.Value == "Please Select An Operation")
	{
	LString = "<script language=JavaScript>alert('No Operation Selected.
\n Please Select An Operation');</script>";
		RegisterStartupScript("startupScript", LString);
		}
	else
		{
		LString = "<script language=JavaScript>alert('" + item.Value +
"');</script>";
		conoper.Open();
		cmdoper.Connection = conoper;
		cmdoper.CommandType = CommandType.Text;
		LString = "select * from WRKINSTRUC.WORKSTATN Where OPERNAME = '" +
item.Value + "'";
		cmdoper.CommandText =  LString;
		OleDbDataReader dsWorkCenters = cmdoper.ExecuteReader();
		WorkStations.DataSource = dsWorkCenters;
		WorkStations.DataTextField = "WORKSTATN";
		WorkStations.DataValueField = "WORKSTATN";
		WorkStations.DataBind();
		conoper.Close();
		WorkStations.Items.Insert(0, new ListItem("Please Select A
Workstation", "0"));
		WorkStations.SelectedIndex = 0;
		}
	}
   }
}

protected void WorkStations_SelectedIndexChanged(object sender,
EventArgs e)  // This is dropdownlist box 2
  {
  string LString;
  int indexNum;
  indexNum = WorkStations.SelectedIndex;
  Response.Write(indexNum );

  foreach(ListItem item in WorkStations.Items)
  {
  if(item.Selected)
	{
	if (item.Value == "Please Select A Workstation")
		{
		LString = "<script language=JavaScript>alert('No Workstation
Selected. \n Please Select A Workstation');</script>";
		RegisterStartupScript("startupScript", LString);
		}
	else
		{
		LString = "<script language=JavaScript>alert('" + item.Text +
"');</script>";
		RegisterStartupScript("startupScript", LString);
		}
	}
}
}


As I am new to C# and ASP.NET could anyone please give me an idea of
what is going wrong here

Thanks in advance for any assistance offered

Iain

8. Controls added dynamically disappears after postback - Asp.Net