473,239 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

populating listbox using jscript and not postback

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()
Nov 18 '05 #1
2 2893
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 asthe page loads. The second listbox get populated based on the user'sselection from the first listbox. However, currently the code issuch that with each selection there is a postback. We want to avoid itusing filter and javascript. I am not using ADO.NET but adodbc and nodatagrids or datasets (please don't tell me that i should as my bossclearly 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 thelistboxes

'select items for second listbox
Dim rsSub As New ADODB.Recordset
rsSub.Open("SELECT sub_name, sub_id FROM subs WHEREcat_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 eAs 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()
.

Nov 18 '05 #2
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;Dat a 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[i] = 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;Dat a 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" <an*******@discussions.microsoft.com> wrote in message news:<05****************************@phx.gbl>...
Sounds like the 'AutoPostback' property for the listbox is
set to True - change it to False...

Jerry

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
2
by: Luis Ferrao | last post by:
Hi, I have some code that looks like this: -- CODE ---------------------------- Public Class NodeEditor Inherits System.Web.UI.Page Protected WithEvents ChildList As...
8
by: Steve Schroeder | last post by:
For some reason I cannot get the OnSelectedIndexChanged event to fire for a listbox I have on a page. I'm able to populate the listbox with data from a stored procedure, but cannot trigger the...
6
by: P K | last post by:
I have a listbox which I am populating on the client (it contains a list of dates selected from calender). The listbox is a server control. When I get to the server after postback by selecting an...
3
by: RFS666 | last post by:
Hello together, I tried to find out about populating an asp.net server control (a dropdownlist) from the clientside jscript, but I didn't find a solution up to now. I cannot use a html...
4
by: collie | last post by:
HI, 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...
8
by: jack-b | last post by:
Hi, I have a list box which displays countries names and a second listbox which displays their cites (based on the selection made in ListBox 1) If the user selects USA i want to display cities...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.