468,103 Members | 1,170 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

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_SelectedIndexCha nged">
</ASP:DropDownList>&nbsp;&nbsp;

<ASP:DropDownList id="WorkStations"
runat="server"
AppendDataBoundItems="True"
AutoPostBack="True"
OnSelectedIndexChanged="WorkStations_SelectedIndex Changed">
</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

Dec 8 '06 #1
3 5023
Are you populating the first dropdownlist at page_load?
if so, is it surrounded by an if/then/postback block?

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
"Iain" <Em**************@gmail.comwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
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_SelectedIndexCha nged">
</ASP:DropDownList>&nbsp;&nbsp;

<ASP:DropDownList id="WorkStations"
runat="server"
AppendDataBoundItems="True"
AutoPostBack="True"
OnSelectedIndexChanged="WorkStations_SelectedIndex Changed">
</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

Dec 8 '06 #2
Hi David

Answer to both questions is Yes.

Iain

Dec 11 '06 #3
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

Dec 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by huzz | last post: by
1 post views Thread by Michael Kolias | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.