Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DropDownList 2 always returns Selected = 0 for all items - even selected item

Reply
Thread Tools

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

 
 
Iain
Guest
Posts: n/a
 
      12-08-2006
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.

<ASPropDownList id="Operation"
runat="server"
AppendDataBoundItems="True"
AutoPostBack="True"
OnSelectedIndexChanged="Operation_SelectedIndexCha nged">
</ASPropDownList>&nbsp;&nbsp;

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

 
Reply With Quote
 
 
 
 
David Wier
Guest
Posts: n/a
 
      12-08-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.
>
> <ASPropDownList id="Operation"
> runat="server"
> AppendDataBoundItems="True"
> AutoPostBack="True"
> OnSelectedIndexChanged="Operation_SelectedIndexCha nged">
> </ASPropDownList>&nbsp;&nbsp;
>
> <ASPropDownList id="WorkStations"
> runat="server"
> AppendDataBoundItems="True"
> AutoPostBack="True"
> OnSelectedIndexChanged="WorkStations_SelectedIndex Changed">
> </ASPropDownList>&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
>



 
Reply With Quote
 
 
 
 
Iain
Guest
Posts: n/a
 
      12-11-2006
Hi David

Answer to both questions is Yes.

Iain

 
Reply With Quote
 
Iain
Guest
Posts: n/a
 
      12-11-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Dropdownlist SelectedItem always returns 1st value! VB Programmer ASP .Net 5 03-11-2008 04:40 PM
Radiobutton always has Checked = true, even when not selected Alan Silver ASP .Net 2 07-20-2006 12:08 PM
dropDownList always returns 0 for selected index Dica ASP .Net 3 10-21-2005 03:30 PM
DropDownList always returns value at index 0 Nathan Sokalski ASP .Net 4 09-18-2005 07:20 AM
DropDownList always returns value at index 0 Nathan Sokalski ASP .Net Web Controls 3 09-18-2005 06:45 AM



Advertisments