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

  • Thread starter Thread starter Iain
  • Start date Start date
I

Iain

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>&nbsp;&nbsp;

<ASP:DropDownList id="WorkStations"
runat="server"
AppendDataBoundItems="True"
AutoPostBack="True"
OnSelectedIndexChanged="WorkStations_SelectedIndexChanged">
</ASP:DropDownList>&nbsp;&nbsp;
..
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
 
Are you populating the first dropdownlist at page_load?
if so, is it surrounded by an if/then/postback block?
 
Hi David

Found the problem.
I had additional lines in the Page Load - if(IsPostBack)
which set the index to 0

I am now away to beat myself with a stick

Thnaks for your interest in my problem anyway

Iain
 
Back
Top