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

Discussion in 'ASP .Net' started by Iain, Dec 8, 2006.

  1. Iain

    Iain Guest

    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
    Iain, Dec 8, 2006
    #1
    1. Advertising

  2. Iain

    David Wier Guest

    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" <> wrote in message
    news:...
    > 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
    >
    David Wier, Dec 8, 2006
    #2
    1. Advertising

  3. Iain

    Iain Guest

    Hi David

    Answer to both questions is Yes.

    Iain
    Iain, Dec 11, 2006
    #3
  4. Iain

    Iain Guest

    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
    Iain, Dec 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    5
    Views:
    8,063
    maysam
    Mar 11, 2008
  2. Dica
    Replies:
    3
    Views:
    6,712
  3. Alan Silver
    Replies:
    2
    Views:
    599
    Alan Silver
    Jul 20, 2006
  4. Chicagoboy27
    Replies:
    1
    Views:
    329
    Chicagoboy27
    Dec 12, 2006
  5. Ryan Liu
    Replies:
    1
    Views:
    2,214
    Alexey Smirnov
    Jan 21, 2009
Loading...

Share This Page