473,770 Members | 5,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="WorkStation s" will not be populated until the first has
been changed
The definitions are below.

<ASP:DropDownLi st id="Operation"
runat="server"
AppendDataBound Items="True"
AutoPostBack="T rue"
OnSelectedIndex Changed="Operat ion_SelectedInd exChanged">
</ASP:DropDownLis t>&nbsp;&nbsp;

<ASP:DropDownLi st id="WorkStation s"
runat="server"
AppendDataBound Items="True"
AutoPostBack="T rue"
OnSelectedIndex Changed="WorkSt ations_Selected IndexChanged">
</ASP:DropDownLis t>&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_Selec tedIndexChanged (object sender, EventArgs
e)
{
OleDbConnection conoper = new OleDbConnection ("provider=IBMD A400;Data
Source=xx.xxx.x x.xxx;User Id=xxxxxxx;pass word=xxxxxxxxxx ");
OleDbCommand cmdoper = new OleDbCommand();
string LString;

foreach(ListIte m item in Operation.Items ) // This is dropdownlist
box 1
{
if(item.Selecte d)
{
if (item.Value == "Please Select An Operation")
{
LString = "<script language=JavaSc ript>alert('No Operation Selected.
\n Please Select An Operation');</script>";
RegisterStartup Script("startup Script", LString);
}
else
{
LString = "<script language=JavaSc ript>alert('" + item.Value +
"');</script>";
conoper.Open();
cmdoper.Connect ion = conoper;
cmdoper.Command Type = CommandType.Tex t;
LString = "select * from WRKINSTRUC.WORK STATN Where OPERNAME = '" +
item.Value + "'";
cmdoper.Command Text = LString;
OleDbDataReader dsWorkCenters = cmdoper.Execute Reader();
WorkStations.Da taSource = dsWorkCenters;
WorkStations.Da taTextField = "WORKSTATN" ;
WorkStations.Da taValueField = "WORKSTATN" ;
WorkStations.Da taBind();
conoper.Close() ;
WorkStations.It ems.Insert(0, new ListItem("Pleas e Select A
Workstation", "0"));
WorkStations.Se lectedIndex = 0;
}
}
}
}

protected void WorkStations_Se lectedIndexChan ged(object sender,
EventArgs e) // This is dropdownlist box 2
{
string LString;
int indexNum;
indexNum = WorkStations.Se lectedIndex;
Response.Write( indexNum );

foreach(ListIte m item in WorkStations.It ems)
{
if(item.Selecte d)
{
if (item.Value == "Please Select A Workstation")
{
LString = "<script language=JavaSc ript>alert('No Workstation
Selected. \n Please Select A Workstation');</script>";
RegisterStartup Script("startup Script", LString);
}
else
{
LString = "<script language=JavaSc ript>alert('" + item.Text +
"');</script>";
RegisterStartup Script("startup Script", 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 5283
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.comwro te in message
news:11******** *************@j 72g2000cwa.goog legroups.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="WorkStation s" will not be populated until the first has
been changed
The definitions are below.

<ASP:DropDownLi st id="Operation"
runat="server"
AppendDataBound Items="True"
AutoPostBack="T rue"
OnSelectedIndex Changed="Operat ion_SelectedInd exChanged">
</ASP:DropDownLis t>&nbsp;&nbsp;

<ASP:DropDownLi st id="WorkStation s"
runat="server"
AppendDataBound Items="True"
AutoPostBack="T rue"
OnSelectedIndex Changed="WorkSt ations_Selected IndexChanged">
</ASP:DropDownLis t>&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_Selec tedIndexChanged (object sender, EventArgs
e)
{
OleDbConnection conoper = new OleDbConnection ("provider=IBMD A400;Data
Source=xx.xxx.x x.xxx;User Id=xxxxxxx;pass word=xxxxxxxxxx ");
OleDbCommand cmdoper = new OleDbCommand();
string LString;

foreach(ListIte m item in Operation.Items ) // This is dropdownlist
box 1
{
if(item.Selecte d)
{
if (item.Value == "Please Select An Operation")
{
LString = "<script language=JavaSc ript>alert('No Operation Selected.
\n Please Select An Operation');</script>";
RegisterStartup Script("startup Script", LString);
}
else
{
LString = "<script language=JavaSc ript>alert('" + item.Value +
"');</script>";
conoper.Open();
cmdoper.Connect ion = conoper;
cmdoper.Command Type = CommandType.Tex t;
LString = "select * from WRKINSTRUC.WORK STATN Where OPERNAME = '" +
item.Value + "'";
cmdoper.Command Text = LString;
OleDbDataReader dsWorkCenters = cmdoper.Execute Reader();
WorkStations.Da taSource = dsWorkCenters;
WorkStations.Da taTextField = "WORKSTATN" ;
WorkStations.Da taValueField = "WORKSTATN" ;
WorkStations.Da taBind();
conoper.Close() ;
WorkStations.It ems.Insert(0, new ListItem("Pleas e Select A
Workstation", "0"));
WorkStations.Se lectedIndex = 0;
}
}
}
}

protected void WorkStations_Se lectedIndexChan ged(object sender,
EventArgs e) // This is dropdownlist box 2
{
string LString;
int indexNum;
indexNum = WorkStations.Se lectedIndex;
Response.Write( indexNum );

foreach(ListIte m item in WorkStations.It ems)
{
if(item.Selecte d)
{
if (item.Value == "Please Select A Workstation")
{
LString = "<script language=JavaSc ript>alert('No Workstation
Selected. \n Please Select A Workstation');</script>";
RegisterStartup Script("startup Script", LString);
}
else
{
LString = "<script language=JavaSc ript>alert('" + item.Text +
"');</script>";
RegisterStartup Script("startup Script", 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
2806
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 Description"></asp:BoundColumn> <asp:TemplateColumn runat="server" HeaderText="Id Type Option" "> <itemtemplate> <asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' /> <asp:label runat="server" ID="LlbTypeOption" Visible=False...
0
2428
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 arbitrary number of empty rows to be added to it. So, if a user knew in advance that they wanted to add 3 phone numbers, they can type the nuber '3' in a textbox (outside the repeater) and click a button (also outside of a repeater), and 3 new empty ...
4
4682
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 this: ddlMyDdl.SelectedItem The problem is that I am ALWAYS getting the first item back! Even when I select the 5th item, the 10th item, etc....
3
6200
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) e.Item.FindControl("DdlTest") enables me to work with ddlTest - with, for example, ddlTest.SelectedValu However, in DataGrid1_EditCommand, ddlTest.SelectedValue returns "Object reference not set to an instance of an object.
18
2456
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 textbox, and in the second row I need to bind data into a dropdownlist...It all depends on the data I select from the database. I cannot use TemplateColumn because it has to be the same type of control for one column.
2
11313
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 database. for example: if the ddl_value is 2 from the database i want the second list item to be selected by default.
10
10771
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 = true; Where endTime is a string containing "15".
1
1376
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 datagrid_Update function I get an Object reference not set to an instance of an object. I am reakky stuck here and I would appreciate any help from you guys.
1
4940
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 time. The problem is that when an item has been removed from the lookup table, and a user wants to retrieve a record that used the deleted item, the following error occurs: 'ddlAssignedTo' has a SelectedValue which is invalid because it...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.