Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating DropDown at Runtime ***URGENT***

Status
Not open for further replies.

ufobaby

MIS
Sep 25, 2001
238
US
hi, am an absolute newbie,
i have 2 dropdown menus. when the first one is clicked the 2nd one should be populated with values from recordset. The problem is when the page is displayed both the dropdowns should be visible. So far have written the below code please help me debug this. it just does not work !!!!
================
<%
Dim ConLog 'For Opening Connection to Db
Dim rs 'For RecordsSet
Dim aryClt
Set ConLog = Server.CreateObject(&quot;adodb.connection&quot;)
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
ConLog.Open &quot;Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\rs.Open &quot;select distinct cod_client from corp_2002 where left(cod_client,1)='A'&quot;,ConLog
aryClt = rs.GetRows()
Response.Write(&quot;Conn estb....&quot;)
Response.Write(&quot;<Script Language=JavaScript>&quot;)
Response.Write(&quot;<!--&quot;)
Response.Write(&quot;function PopulateOptions(){&quot; & chr(13))
Response.Write(&quot;Conn estb 123....&quot;)
Response.Write(&quot;x= 0&quot;)
Response.Write(&quot;for x=0 to ubound(aryclt,2)&quot;)
Response.Write(&quot; sSelect = sSelect + '<OPTION VALUE=' + aryClt(1,x) + '>' + aryClt(0,x) + '</OPTION>'&quot; & chr(13))
Response.Write(&quot;Next&quot;)

Response.Write(&quot; document.all['fill'].innerHTML = sSelect;&quot; & chr(13))
Response.Write(&quot;}&quot;)
Response.Write(&quot;-->&quot;)
Response.Write(&quot;</Script>&quot;)
%>
<html>
<body>
<form method=&quot;post&quot; action=&quot;results.asp&quot;>
<P><select name=&quot;reptype&quot; size=&quot;1&quot; onChange=PopulateOptions()>
<OPTION VALUE=&quot;none&quot; SELECTED=&quot;Selected&quot;>Select Report Type</option>
<OPTION VALUE=&quot;Location&quot;>Location Wise</option>
<OPTION VALUE=&quot;Corp&quot;>Corp Code Wise</option>
</select></P>
<P>
<SPAN Id=fill>
<select name=&quot;cmbFill&quot; size=&quot;1&quot;>
<option value=0 selected></option>
</select>
</span>
</P>
<P>
<input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;>
<input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;>
</P>
</form>
</body>
</html>
=========================
You can also mail me at kniraj@hotmail.com if u need the database, but in a plain asp code the &quot;for loop&quot; in bold works fine to display data.

PS. Also if u can direct me to some site showing how to use JS and ASP together, it would be of great help.

Regards
Niraj [noevil]
 
<%
Dim ConLog 'For Opening Connection to Db
Dim rs 'For RecordsSet
Dim aryClt
Set ConLog = Server.CreateObject(&quot;adodb.connection&quot;)
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
ConLog.Open &quot;Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\rs.Open &quot;select distinct cod_client from corp_2002 where left(cod_client,1)='A'&quot;,ConLog
aryClt = rs.GetRows()
Response.Write(&quot;Conn estb....&quot;)
Response.Write(&quot;<Script Language=JavaScript>&quot;)
Response.Write(&quot;<!--&quot;)
Response.Write(&quot;function PopulateOptions(){&quot; & chr(13))
Response.Write &quot;//Conn estb 123....&quot; & vbcrlf

Response.Write &quot;var sSelect='<select name=cmbFill>'&quot; & vbcrlf
x= 0

for x=0 to ubound(aryclt,2)

sSelect = sSelect + &quot;<OPTION VALUE='&quot;& aryClt(0,x) & &quot; '> &quot; & aryClt(1,x) & &quot; </OPTION>&quot;

next

Response.Write &quot;var sSelect=sSelect + &quot; & chr(34) & sSelect & chr(34) & vbcrlf
Response.Write &quot;var sSelect=sSelect + '</select>'&quot; & vbcrlf

Response.Write &quot; document.all['fill'].innerHTML =sSelect&quot; & vbcrlf

Response.Write(&quot;}&quot;)
Response.Write(&quot;-->&quot;)
Response.Write(&quot;</Script>&quot;)
%>
<html>
<body>
<form method=&quot;post&quot; action=&quot;results.asp&quot;>
<P><select name=&quot;reptype&quot; size=&quot;1&quot; onChange=PopulateOptions()>
<OPTION VALUE=&quot;none&quot; SELECTED=&quot;Selected&quot;>Select Report Type</option>
<OPTION VALUE=&quot;Location&quot;>Location Wise</option>
<OPTION VALUE=&quot;Corp&quot;>Corp Code Wise</option>
</select></P>
<P>
<SPAN Id=fill>
<select name=&quot;cmbFill&quot; size=&quot;1&quot;>
<option value=0 selected></option>
</select>
</span>
</P>
<P>
<input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;>
<input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;>
</P>
</form>
</body>
</html>
 
Thanks a ton SarkMan for u r time,
but now it does not execute beyond
Response.Write(&quot;Conn estb....&quot;)
the previous code did show both the dropdowns and buttons.

Do i write
Response.Write &quot;var sSelect='<select name=cmbFill>'&quot; & vbcrlf
and the loop on the same line or the way u have written. I believe the problem is with the way we have concatenated the string. And why do we again need
sSelect='<select name=cmbFill>' as we already have created the dropdown in thehtml code below.

Please Help !!!
Thanks & Regards
Niraj [noevil]
 
Try the following code...You don't need to use JavaScript to populate your combo box. Just loop through recordset.

<%
'----------New Code ------------------------
Dim rs 'For RecordsSet
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.ActiveConnection = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\ OLEDB:System Database=system.mdw;&quot;
rs.Source = &quot;select distinct cod_client from corp_2002 where left(cod_client,1)='A'&quot;
rs.CursorType = 0
rs.CursorLocation = 2
rs.LockType = 3
rs.Open()




'----------Don't need all this ------------------
'Dim ConLog 'For Opening Connection to Db
'Dim rs 'For RecordsSet
'Dim aryClt
'Set ConLog = Server.CreateObject(&quot;adodb.connection&quot;)
'Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
'ConLog.Open &quot;Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\'rs.Open &quot;select distinct cod_client from corp_2002 where left(cod_client,1)='A'&quot;,ConLog
'aryClt = rs.GetRows()
'Response.Write(&quot;Conn estb....&quot;)
'Response.Write(&quot;<Script Language=JavaScript>&quot;)
'Response.Write(&quot;<!--&quot;)
'Response.Write(&quot;function PopulateOptions(){&quot; & chr(13))
'Response.Write(&quot;Conn estb 123....&quot;)
'Response.Write(&quot;x= 0&quot;)
'Response.Write(&quot;for x=0 to ubound(aryclt,2)&quot;)
'Response.Write(&quot; sSelect = sSelect + '<OPTION VALUE=' + aryClt(1,x) + '>' + aryClt(0,x) + '</OPTION>'&quot; & chr(13))
'Response.Write(&quot;Next&quot;)
'Response.Write(&quot; document.all['fill'].innerHTML = sSelect;&quot; & chr(13))
'Response.Write(&quot;}&quot;)
'Response.Write(&quot;-->&quot;)
'Response.Write(&quot;</Script>&quot;)
'----------------------------------------------------


%>
<html>
<body>
<form method=&quot;post&quot; action=&quot;results.asp&quot;>
<P><select name=&quot;reptype&quot; size=&quot;1&quot;>
<OPTION VALUE=&quot;none&quot; SELECTED>Select Report Type</option>
<OPTION VALUE=&quot;Location&quot;>Location Wise</option>
<OPTION VALUE=&quot;Corp&quot;>Corp Code Wise</option>
</select>
</P>


<!-- New way to fill combo box with Recordset results. -->

<P><SELECT NAME=&quot;cboWhatever&quot;>
<OPTION VALUE=0 SELECTED>-make selection-</OPTION>
<%
While (NOT rs.EOF)
%>
<OPTION VALUE=&quot;<%=(rs.Fields.Item(&quot;cod_Client&quot;).Value)%>&quot; ><%=(rs.Fields.Item(&quot;cod_Client&quot;).Value)%></OPTION>
<%
rs.MoveNext()
Wend
%>
</SELECT>




<!-- Your old way to fill results.
<SPAN Id=fill>
<select name=&quot;cmbFill&quot; size=&quot;1&quot;>
<option value=0 selected></option>
</select>
</span>
</P>
-->



<P>
<input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;>
<input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;>
</P>
</form>
</body>
</html>
 
Thanks, cbchumley for the help,
but the problem is that the Both the Drop Down boxes ahould be visible when the form loads. The one which we are filling with values will be displayed blank initially, and will be filled only onChange of the First Combo.

Any Ideas on this..... Please Help

Regards
Niraj [noevil]
 
ufobaby, do you already know what values you are going to populate in the combo boxes regardless of what users selects from first combo box or are you wanting to send page back up to server passing the value of first combo box to a SQL(WHERE)clause to then fill second combo box?

cbchumley
[yinyang]
 
no, would be keeping the table name in the first dropdown <Option Value=tabname>Text</Option>

and use this in the where clause. So the Recordset also will be dynamically created and would be run each time the user selects the First Combo. Actually i will be having about 4 Dropdown which will get populated with on single recordset. Have taken only one in the above example.

Regards
Niraj [noevil]
 
You can try this, it uses an array that is filled with nothing at first until you make a selection from first combo box.
Once selection made you are passing a querystring to the same page with table name that is used in SQL string.
Then array is filled with results and reiterated to populate second combo box.
Hope this helps...cbchumley

<%
If Request.QueryString(&quot;Table&quot;) <> &quot;&quot; Then
Dim sTable
sTable = Request.QueryString(&quot;Table&quot;)

Dim rs 'For RecordsSet
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.ActiveConnection = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\ OLEDB:System Database=system.mdw;&quot;
rs.Source = &quot;select distinct cod_client from &quot; & sTable & &quot; where left(cod_client,1)='A'&quot;
rs.CursorType = 0
rs.CursorLocation = 2
rs.LockType = 3
rs.Open()

iCount = rs.RecordCount
Dim aList()
ReDim aList(iCount,1)

For i = 1 to iCount
aList(i,1) = rs(&quot;cod_client&quot;)
rs.MoveNext
Next

rs.Close
Set rs = Nothing

Else

Dim aList()
ReDim aList(1,1)

For i = 1 to 1
aList(i,1) = &quot;&quot;
Next

End If
%>

<html>

<SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript>
<!--

Sub cboTable_onchange

Dim MyIndex
MyIndex = frmTableSearch.cboTable.selectedIndex

self.location.href = (frmTableSearch.cboTable.options MyIndex).value)
End Sub

-->
</SCRIPT>

<body>
<form name=&quot;frmTableSearch&quot; method=&quot;post&quot; action=&quot;results.asp&quot;>
<P><select name=&quot;cboTable&quot; size=&quot;1&quot;>
<OPTION VALUE=&quot;none&quot; SELECTED>Select Report Type</option>
<OPTION VALUE=&quot;thispage.asp?Table=Location&quot;>Location Wise</option>
<OPTION VALUE=&quot;thispage.asp?Table=Corp&quot;>Corp Code Wise</option>
</select>
</P>

<P>
<SELECT NAME=&quot;cboWhatever&quot;>
<OPTION VALUE=0 SELECTED>-make selection-</OPTION>
<%
For i = 1 to ubound(aList)
Response.Write &quot;<OPTION VALUE=&quot; & aList(i,1) & &quot;>&quot; & aList(i,1) & &quot;</OPTION>&quot;
Next
%>
</SELECT>
</P>

<P>
<input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;>
<input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;>
</P>
</form>
</body>
</html>
 
thanks,
i tired but the location.href (i.e the url) of the page does not change and so the values in the combo 2 also does not get populated. The file name is &quot;options1.asp&quot; and have replaced &quot;thispage.asp&quot;. Do i need to give the full path ???

Could you please help... i think the change event is not getting fired or some thing...

Regards
Niraj [noevil]
 
I had the same problem. I ended up putting the drop downs on two different pages but used a frame so it appears they are on the same page. Now when the first drop down is selected it refreshes the second page and populates the second one drop down. It actually turned out alright. You cannot tell the difference.

I was told this is the only way to do it because the page has to be refreshed in order to query the DB again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top