472,782 Members | 1,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

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 5221
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
0
by: kamaumalone | last post by:
I have a dropdownlist which lives inside of a repeater. The repeater accepts user input via textboxes and the aforementioned dropdownlist. The repeater accepts phone numbers and allows for an...
4
by: VB Programmer | last post by:
I have a dropdownlist which I populated on page_load. This works fine. I simply do a ddlMyDdl.Items.Add(xxx) I also have a button which references the CURRENTLY selected item in the ddl like...
3
by: mg | last post by:
I have a DataGrid (WebForm - C#) that has a template column that contains a dropdownlist named "DdlTest" In DataGrid1_UpdateCommand, the lin DropDownList ddlTest = (DropDownList)...
18
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a...
2
by: huzz | last post by:
How do i make a dropdownlist selected value based on the value i retrive from the database. Basically i have an edit page and like to display the default value in a dropdown list from the...
10
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
1
by: Michael Kolias | last post by:
Hi everybody, I am having a problem getting the selected value of a drop down list that is populated dynamically inside a datagrid control. When I try to access the selected item on the...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.