populating listbox using jscript and not postback

Discussion in 'ASP .Net' started by collie, Nov 20, 2003.

  1. collie

    collie Guest

    Hi,

    I have 2 listboxes. The first gets populated from the db as soon as
    the page loads. The second listbox get populated based on the user's
    selection from the first listbox. However, currently the code is
    such that with each selection there is a postback. We want to avoid it
    using filter and javascript. I am not using ADO.NET but adodbc and no
    datagrids or datasets (please don't tell me that i should as my boss
    clearly doesn't want to get into it at this stage).

    How can i do it?

    Thanks


    Currently the code with the postback is as follows:
    page_load

    if not page.ispostback then
    Do While Not rs.EOF
    li = New ListItem(rs("cat_name").Value,
    rs("cat_id").Value)
    List1.Items.Add(li)
    rs.MoveNext()
    Loop

    End If

    'List1.SelectedIndex = 0
    rs.Close()
    rs = Nothing

    List1.SelectedValue = cat_id

    'here there is some other code not relevant to the
    listboxes

    'select items for second listbox
    Dim rsSub As New ADODB.Recordset
    rsSub.Open("SELECT sub_name, sub_id FROM subs WHERE
    cat_id=" & cat_id.ToString & " ORDER BY sub_name ASC", cn)
    Dim l_item As ListItem
    Do While Not rsSub.EOF
    l_item = New ListItem(rsSub("sub_name").Value,
    rsSub("sub_id").Value)
    List2.Items.Add(l_item)
    rsSub.MoveNext()
    Loop

    List2.SelectedValue = sub_id

    rsSub.Close()
    rsSub = Nothing


    cn.Close()
    End If
    ' End If


    End If

    End Sub

    Protected Sub Select1Change(ByVal sender As System.Object, ByVal e
    As System.EventArgs)

    Dim cn As New ADODB.Connection
    cn.Open(YBayTools.Constants.ConnectionString)


    Dim rs As New ADODB.Recordset
    Dim li As ListItem

    rs.Open("Select * from subs where cat_ID =" &
    List1.SelectedItem.Value.ToString, cn)
    'rs.Open("Select * from subs where cat_ID ='" &
    List1.SelectedItem.Text.ToString & "' &
    List1.SelectedItem.value.ToString", cn)


    Dim strCat As String

    If List2.Items.Count > 0 Then
    List2.Items.Clear()
    End If

    If rs.BOF And rs.EOF Then
    Response.Write("no records found")
    Else

    Do While Not rs.EOF
    li = New ListItem(rs("sub_name").Value,
    rs("sub_id").Value)
    List2.Items.Add(li)
    rs.MoveNext()

    Loop


    End If

    rs.Close()
    rs = Nothing

    cn.Close()
     
    collie, Nov 20, 2003
    #1
    1. Advertising

  2. collie

    Jerry Guest

    Sounds like the 'AutoPostback' property for the listbox is
    set to True - change it to False...

    Jerry
    >-----Original Message-----
    >Hi,
    >
    >I have 2 listboxes. The first gets populated from the db

    as soon as
    >the page loads. The second listbox get populated based on

    the user's
    >selection from the first listbox. However, currently

    the code is
    >such that with each selection there is a postback. We

    want to avoid it
    >using filter and javascript. I am not using ADO.NET but

    adodbc and no
    >datagrids or datasets (please don't tell me that i should

    as my boss
    >clearly doesn't want to get into it at this stage).
    >
    >How can i do it?
    >
    >Thanks
    >
    >
    >Currently the code with the postback is as follows:
    >page_load
    >
    >if not page.ispostback then
    > Do While Not rs.EOF
    > li = New ListItem(rs

    ("cat_name").Value,
    >rs("cat_id").Value)
    > List1.Items.Add(li)
    > rs.MoveNext()
    > Loop
    >
    > End If
    >
    > 'List1.SelectedIndex = 0
    > rs.Close()
    > rs = Nothing
    >
    > List1.SelectedValue =

    cat_id
    >
    > 'here there is some other code not

    relevant to the
    >listboxes
    >
    > 'select items for second listbox
    > Dim rsSub As New ADODB.Recordset
    > rsSub.Open("SELECT sub_name, sub_id FROM

    subs WHERE
    >cat_id=" & cat_id.ToString & " ORDER BY sub_name ASC", cn)
    > Dim l_item As ListItem
    > Do While Not rsSub.EOF
    > l_item = New ListItem(rsSub

    ("sub_name").Value,
    >rsSub("sub_id").Value)
    > List2.Items.Add(l_item)
    > rsSub.MoveNext()
    > Loop
    >
    > List2.SelectedValue = sub_id
    >
    > rsSub.Close()
    > rsSub = Nothing
    >
    >
    > cn.Close()
    > End If
    > ' End If
    >
    >
    > End If
    >
    > End Sub
    >
    > Protected Sub Select1Change(ByVal sender As

    System.Object, ByVal e
    >As System.EventArgs)
    >
    > Dim cn As New ADODB.Connection
    > cn.Open(YBayTools.Constants.ConnectionString)
    >
    >
    > Dim rs As New ADODB.Recordset
    > Dim li As ListItem
    >
    > rs.Open("Select * from subs where cat_ID =" &
    >List1.SelectedItem.Value.ToString, cn)
    > 'rs.Open("Select * from subs where cat_ID ='" &
    >List1.SelectedItem.Text.ToString & "' &
    >List1.SelectedItem.value.ToString", cn)
    >
    >
    > Dim strCat As String
    >
    > If List2.Items.Count > 0 Then
    > List2.Items.Clear()
    > End If
    >
    > If rs.BOF And rs.EOF Then
    > Response.Write("no records found")
    > Else
    >
    > Do While Not rs.EOF
    > li = New ListItem(rs("sub_name").Value,
    >rs("sub_id").Value)
    > List2.Items.Add(li)
    > rs.MoveNext()
    >
    > Loop
    >
    >
    > End If
    >
    > rs.Close()
    > rs = Nothing
    >
    > cn.Close()
    >.
    >
     
    Jerry, Nov 20, 2003
    #2
    1. Advertising

  3. collie

    collie Guest

    Hi,

    If i change the autopastback to false then it doesn't get the data
    from the db. I need to rewrite the code using jscript.

    I need to populate 2 listboxes from a db. When the page loads then the
    first listbox needs to be populated and based on selection from that
    listbox the second listbox needs to be populated accordingly with the
    matching items.
    However, my boss doesn't want the page to do a postback once an item
    from the first listbox is selected. also, he doesn't want to use
    ado.net but classic ado.
    He wants to use jscript. I am writing my code in vb.net

    I wrote a code but only the first listbox gets populated and when i
    select an item then that item appears in the second listbox eg. if i
    selected cars from the 1st listbox then cars will appear in the 2nd
    listbox instead of BMW, HONDA etc.

    I know that my code might be totally wrong as i have no idea what to
    do.

    My db has 2 tables called CATS with CAT_NAME (Such as cars) and
    CAT_ID. The 2nd table is called SUBS and contains SUB_ID, CAT_ID and
    SUB_NAME (such as Honda, BMW).It is very important that my code with
    read the CAT_ID and SUB_ID as i have to use them later on.


    Can someone please help me?

    Thanks


    Here is my code:

    Dim objconn As New ADODB.Connection()
    Dim rsx As New ADODB.Recordset()
    Dim sLastManufacturer
    Dim manufacturer As New ListBox()
    objConn = Server.CreateObject("adodb.connection")
    objConn.OPEN("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    Server.MapPath("/duclassified.mdb"))
    rsX = Server.CreateObject("ADODB.Recordset")
    rsx.Open("SELECT CAT_NAME, CAT_ID FROM CATS", objConn)

    If rsX.EOF Then
    Response.Write("No category.<BR>")

    Else
    ' write the CATEGORY listbox...

    Response.Write("<SELECT NAME=""manufacturer"" SIZE=15" & _
    " ONCHANGE=""manuselected(this);"" >")
    ' write the entry code for the javascript...
    Dim sJavaScript = "function manuselected(elem){" &
    Environment.NewLine & _
    "for (var i = model." & _
    "options.length; i >= 0; i--){" & Environment.NewLine & _
    "model.options = null;" & _
    Environment.NewLine
    ' loop through the recordset...
    Do Until rsx.EOF
    ' is this a new manufacturer?
    Dim cat_names = rsx("cat_name").Value

    If (sLastManufacturer) <> "CAT_Names" Then
    ' if so, add an entry to the first listbox
    sLastManufacturer = rsx("CAT_Name").Value
    Response.Write("<OPTION VALUE=" & rsx("CAT_ID").Value & ">"
    & sLastManufacturer & "</OPTION>")
    ' and add a new section to the javascript...
    sJavaScript = sJavaScript & "}" &
    Environment.NewLine & "if (elem.options[elem.selectedIndex].value==" &
    _
    rsx("CAT_ID").Value & "){" & Environment.NewLine
    & ""
    End If
    ' and add a new model line to the javascript...
    sJavaScript = sJavaScript & _
    "model.options[" & _
    "model.options.length] = new Option('" & _
    rsx("CAT_NAME").Value & "','" & rsx("CAT_ID").Value
    & _
    "');" & _
    Environment.NewLine
    rsx.MoveNext()
    Loop
    ' finish the manufacturer listbox...
    Response.Write("</SELECT>")

    rsx.Close()
    rsx = Nothing
    objconn.Close()
    objconn = Nothing

    ' create the SUBS listbox...

    Dim rsSubs As New ADODB.Recordset()
    objconn = Server.CreateObject("adodb.connection")
    objconn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    Server.MapPath("/duclassified.mdb"))
    rsSubs = Server.CreateObject("ADODB.Recordset")
    rsSubs.Open("SELECT SUB_NAME, SUB_ID from SUBS where cat_ID =" &
    manufacturer.SelectedItem.Value.ToString, objconn)
    Response.Write("<SELECT NAME=""model"" SIZE=15>")
    Response.Write("<OPTION>[none currently selected]</OPTION>")
    Response.Write("</SELECT>")
    ' put the last line on the javascript...
    ' and write it out...
    sJavaScript = sJavaScript & Environment.NewLine & "}" &
    Environment.NewLine & _
    "}" & Environment.NewLine
    Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" &
    Environment.NewLine)
    Response.Write(sJavaScript & Environment.NewLine & "</SCRIPT>" &
    Environment.NewLine)
    End If
    End Sub

    "Jerry" <> wrote in message news:<05b101c3af79$239bc2e0$>...
    > Sounds like the 'AutoPostback' property for the listbox is
    > set to True - change it to False...
    >
    > Jerry
     
    collie, Nov 22, 2003
    #3
  4. collie

    manisekarant

    Joined:
    Jul 21, 2006
    Messages:
    1
    Change the following line in your code
    If (sLastManufacturer) <> "CAT_Names" Then
    as
    If (sLastManufacturer) <> CAT_Names Then
     
    manisekarant, Jul 21, 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. Jon Maz
    Replies:
    4
    Views:
    1,284
    Jon Maz
    Sep 9, 2004
  2. Maxwell2006
    Replies:
    5
    Views:
    2,415
    Steven Cheng[MSFT]
    Mar 7, 2006
  3. collie

    listbox selectedindex using jscript

    collie, Nov 30, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    119
    collie
    Nov 30, 2003
  4. John Bentley
    Replies:
    14
    Views:
    368
    Jim Ley
    Feb 5, 2004
  5. VK
    Replies:
    4
    Views:
    363
    Thomas 'PointedEars' Lahn
    Jan 12, 2006
Loading...

Share This Page