Multiple select listbox values in query

Discussion in 'ASP .Net Web Controls' started by DC Gringo, Apr 12, 2004.

  1. DC Gringo

    DC Gringo Guest

    I have two listboxes, the first of which is an autopostback=true that allows
    multiple row selection. When I select multiple values (by holding down CTL)
    in the first one, it should query the second one. I seem unable to do this
    as it only sends back the first item I select whether I have the CTL key
    down or not. Upon the return trip, I can select another one, but it still
    only sends value for the last one I selected.

    -- MY LISTBOX --

    <asp:ListBox ID="lbProvinces" runat="server" Font-Size="8pt" width="150px"
    EnableViewState="true" OnSelectedIndexChanged="ddlQueryDistricts"
    autopostback="true" SelectionMode="Multiple" Rows="3"></asp:ListBox>


    -- MY CODE --

    Sub ddlQueryDistricts(ByVal sender As Object, ByVal e As EventArgs)

    Dim conString As String =
    "server=myserver;database=mydb;uid=myuser;pwd=mypwd;"

    Dim _sqlStmtLbDistricts As String

    If lbProvinces.SelectedItem.Value <> "0" Then
    _sqlStmtLbDistricts = "SELECT tblDistrict.clnGUID, tblDistrict.clnName
    FROM tblDistrict WHERE tblDistrict.clnProvinceGUID = '" &
    lbProvinces.SelectedItem.Value & "' ORDER BY tblDistrict.clnName"
    Else
    _sqlStmtLbDistricts = "SELECT tblDistrict.clnGUID, tblDistrict.clnName
    FROM tblDistrict ORDER BY tblDistrict.clnName"
    End If

    Dim myDataSetDistricts As New DataSet
    Dim myDataAdapterDistricts As New
    SqlDataAdapter(_sqlStmtLbDistricts, conString)
    myDataAdapterDistricts.Fill(myDataSetDistricts, "DistrictsTmp")
    lbDistricts.Datasource = myDataSetDistricts.Tables("DistrictsTmp")
    lbDistricts.DataMember = "DistrictsTmp"
    lbDistricts.DataTextField = "clnName"
    lbDistricts.DataValueField = "clnGUID"
    lbDistricts.DataBind()
    lbDistricts.Items.Insert(0,New ListItem("--ALL","0"))

    End Sub
    --
    _____
    DC G
     
    DC Gringo, Apr 12, 2004
    #1
    1. Advertising

  2. DC Gringo

    Jos Guest

    DC Gringo wrote:
    > I have two listboxes, the first of which is an autopostback=true that
    > allows multiple row selection. When I select multiple values (by
    > holding down CTL) in the first one, it should query the second one.
    > I seem unable to do this as it only sends back the first item I
    > select whether I have the CTL key down or not. Upon the return trip,
    > I can select another one, but it still only sends value for the last
    > one I selected.


    You need to make a loop to find out which items are selected:

    Dim li As ListItem
    For Each li In lbProvinces.Items
    If li.Selected Then
    ' handle li.Value here
    End If
    Next li

    --

    Jos Branders
     
    Jos, Apr 12, 2004
    #2
    1. Advertising

  3. To autopostback your listbox uses clientside JavaScript, which fires a click
    event exactly on the item you just clicked and placing its value as an event
    argument. So you can't send more than one value. How can you determine,
    clicking on which item script should make a decisiion, that it is time to
    collect all selected values and send them back? So simply never use
    autopostback on multiselect listboxes :)

    "DC Gringo" <> wrote in message
    news:...
    > I have two listboxes, the first of which is an autopostback=true that

    allows
    > multiple row selection. When I select multiple values (by holding down

    CTL)
    > in the first one, it should query the second one. I seem unable to do

    this
    > as it only sends back the first item I select whether I have the CTL key
    > down or not. Upon the return trip, I can select another one, but it still
    > only sends value for the last one I selected.
    >
    > -- MY LISTBOX --
    >
    > <asp:ListBox ID="lbProvinces" runat="server" Font-Size="8pt" width="150px"
    > EnableViewState="true" OnSelectedIndexChanged="ddlQueryDistricts"
    > autopostback="true" SelectionMode="Multiple" Rows="3"></asp:ListBox>
    >
    >
    > -- MY CODE --
    >
    > Sub ddlQueryDistricts(ByVal sender As Object, ByVal e As EventArgs)
    >
    > Dim conString As String =
    > "server=myserver;database=mydb;uid=myuser;pwd=mypwd;"
    >
    > Dim _sqlStmtLbDistricts As String
    >
    > If lbProvinces.SelectedItem.Value <> "0" Then
    > _sqlStmtLbDistricts = "SELECT tblDistrict.clnGUID, tblDistrict.clnName
    > FROM tblDistrict WHERE tblDistrict.clnProvinceGUID = '" &
    > lbProvinces.SelectedItem.Value & "' ORDER BY tblDistrict.clnName"
    > Else
    > _sqlStmtLbDistricts = "SELECT tblDistrict.clnGUID, tblDistrict.clnName
    > FROM tblDistrict ORDER BY tblDistrict.clnName"
    > End If
    >
    > Dim myDataSetDistricts As New DataSet
    > Dim myDataAdapterDistricts As New
    > SqlDataAdapter(_sqlStmtLbDistricts, conString)
    > myDataAdapterDistricts.Fill(myDataSetDistricts, "DistrictsTmp")
    > lbDistricts.Datasource = myDataSetDistricts.Tables("DistrictsTmp")
    > lbDistricts.DataMember = "DistrictsTmp"
    > lbDistricts.DataTextField = "clnName"
    > lbDistricts.DataValueField = "clnGUID"
    > lbDistricts.DataBind()
    > lbDistricts.Items.Insert(0,New ListItem("--ALL","0"))
    >
    > End Sub
    > --
    > _____
    > DC G
    >
    >
     
    Viktor Jevdokimov, Apr 14, 2004
    #3
    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. DC Gringo

    Multiple select listbox values in query

    DC Gringo, Apr 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    12,670
    Viktor Jevdokimov
    Apr 14, 2004
  2. K B
    Replies:
    2
    Views:
    702
  3. Seb
    Replies:
    0
    Views:
    592
  4. DC Gringo

    Multiple select listbox values in query

    DC Gringo, Apr 12, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    154
    Viktor Jevdokimov
    Apr 14, 2004
  5. palmiere
    Replies:
    1
    Views:
    472
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page