Page Post Back -- how to retain selecteditem.value of TWO dropdowns???

Discussion in 'ASP .Net' started by KathyB, Jun 26, 2003.

  1. KathyB

    KathyB Guest

    Hi,

    On Page Load (if not postback), the user selects a choice from
    dropdownlist1.

    On SelectedItemChanged for dropdownlist1, dropdownlist2 is populated
    and the user selects an item.

    I cannot find a combination where I can RETAIN both values during
    postback...if I put if not posback on the change event of
    dropdownlist1, it doesn't populate #2...

    Any help appreciated!

    Kathy
     
    KathyB, Jun 26, 2003
    #1
    1. Advertisements

  2. What does your event handler for SelectedItemChanged for dropdownlist1 look
    like?

    Todd Thompson
     
    Todd Thompson, Jun 26, 2003
    #2
    1. Advertisements

  3. KathyB

    Kathy Burke Guest

    Here are both pageload and selecteditemchanged...when I load the page,
    it let's me select the Customer (control 1), and DOES filter the second
    control, but at the same time, resets control 1 to "Select
    Customer"...ugh.

    Both controls set to autopostback and enable viewstate.

    Any ideas?

    Thanks.
    KathyBurke

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here

    If Not IsPostBack Then
    Dim Conn1 As OleDbConnection
    Dim Rdr1 As OleDbDataReader
    Dim Cmd1 As OleDbCommand
    Dim strSQL As String
    Conn1 = New OleDbConnection(strConn)
    strSQL = "SELECT DISTINCT Customer FROM tblCustomers ORDER
    BY Customer"
    Cmd1 = New OleDbCommand(strSQL, Conn1)
    Conn1.Open()
    Rdr1 = Cmd1.ExecuteReader()
    cboCust.DataSource = Rdr1
    cboCust.DataBind()
    cboCust.Items.Insert(0, "Select Customer")
    cboCust.SelectedIndex = 0
    Rdr1.Close()
    Conn1.Close()
    End If

    End Sub

    Private Sub cboCust_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    cboCust.SelectedIndexChanged

    Dim Conn2 As New OleDbConnection()
    Dim Rdr2 As OleDbDataReader
    Dim strSQL2 As String = "SELECT Assy FROM tblAssy WHERE
    ([Customer] = @customer) ORDER BY Assy"
    Dim Cmd2 As New OleDbCommand(strSQL2, Conn2)
    Conn2 = New OleDbConnection(strConn)
    Dim prmCustomer As OleDbParameter = New
    OleDbParameter("@customer", OleDbType.VarChar, 50)
    prmCustomer.Value = cboCust.SelectedItem.Value
    Cmd2.Parameters.Add(prmCustomer)
    Cmd2.Connection = Conn2
    Conn2.Open()
    Rdr2 = Cmd2.ExecuteReader()
    cboAssy.DataSource = Rdr2
    cboAssy.DataBind()
    cboAssy.Items.Insert(0, "Select Assembly")
    cboCust.SelectedIndex = 0
    Rdr2.Close()
    Conn2.Close()
    End Sub
     
    Kathy Burke, Jun 27, 2003
    #3
  4. In your handler you have the following line:



    If I'm understanding your code and problem correctly, you are setting the
    index of the drop down to the first item instead of leaving it set to its
    previous value.


    Todd Thompson
     
    Todd Thompson, Jun 27, 2003
    #4
  5. You are correct in that the code contained within the Page_Load function and
    in the IF NOT POSTBACK conditional won't be run.

    But in your event handler cboCust_SelectedIndexChanged, you are setting the
    cboCust.SelectedIndex=0 which will affect that drop down on the current
    page.

    Todd Thompson
     
    Todd Thompson, Jun 27, 2003
    #5
  6. KathyB

    Kathy Burke Guest

    Todd,

    I'm an idiot...I copied the code for cboAssy but forgot to change that
    control ref...THANK YOU...

    KathyBurke
     
    Kathy Burke, Jun 27, 2003
    #6
  7. Kathy,

    No problem, I've done the same thing myself.

    Glad to be of help.

    Todd
     
    Todd Thompson, Jun 27, 2003
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.