473,378 Members | 1,427 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,378 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 2897
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.