Solved

<select> OnChange event

Posted on 2001-08-15
13
636 Views
Last Modified: 2008-03-04
Hi Guys
I am creating an ASP form with 2 dropdown lists - a list of 'Clients' and a list of users.
I want the users dropdown list tobe populated only after a 'client' is selected.
I a trying to specify the 'OnChange' event for the <SELECT> specifying the function 'RetrieveUsers'
When I select a client the ASP page crashes. The message returned is 'RetrieveUsers' is undefined.
Maybe the syntax for the way I have specified OnChange is wrong or something????
I have posted the script below so as you can get a feel for what I am doing

Cheers

B Cunney

<SELECT NAME="CLIENT" OnChange=RetrieveUsers>
<%
Dim cn
Dim adoRSC
Dim stSQLC
Dim strClient
Dim intClientID

Set cn = Server.CreateObject("ADODB.Connection")
Set adoRSC = Server.CreateObject("ADODB.Recordset")

stSQLC = "Select [CUSTOMER_ID],[NAME] from CUSTOMERS WHERE [NAME] IS NOT NULL order by [NAME] asc"

'Open a connection; the string referes to the DSN
cn.ConnectionString = "DRIVER=SQL Server" &_
    ";SERVER=SOCKS" & _
    ";UID=sa" & _
    ";PWD=moses" & _
    ";DATABASE=FINSYS"
cn.Open

' Associate the Recordset with the open connection
adoRSC.ActiveConnection = cn
adoRSC.Open stSQLC,cn

adoRSC.MoveFirst
 
Do While Not adoRSC.EOF
   
  intClientID = Server.HTMLEncode(adoRSC.Fields("CUSTOMER_ID"))
  strClient = Server.HTMLEncode(Trim(adoRSC.Fields("NAME")))%>
 
  <OPTION VALUE ="<%=intClientID%>"><%=strClient%>
 
  <%adoRSC.MoveNext

Loop%>

</SELECT><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </span>
<SELECT id=select1 name="USER">

<%
Function RetrieveUsers()
Dim adoRSU
Dim stSQLU
Dim strUser
Dim intUserID

Set adoRSU = Server.CreateObject("ADODB.Recordset")

stSQLU = "Select [USER_ID],[USER_NAME] from USERS WHERE [USER_NAME] IS NOT NULL order by [USER_NAME] asc"

' Associate the Recordset with the open connection
adoRSU.ActiveConnection = cn
adoRSU.Open stSQLU,cn

adoRSU.MoveFirst
 
Do While Not adoRSU.EOF
   
  intUserID = Server.HTMLEncode(adoRSC.Fields("USER_ID"))
  strUser = Server.HTMLEncode(Trim(adoRSC.Fields("USER_NAME")))%>
 
  <OPTION VALUE ="<%=intUserID%>"><%=strUser%>
 
  <%adoRSC.MoveNext

Loop

End Function%>

</SELECT></h2>
0
Comment
Question by:Barry Cunney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 2

Expert Comment

by:MCM
ID: 6388833
BCUNNEY, you're on the wrong track. RetreiveUsers is server side script, onChange fires on the client. try this instead:
0
 
LVL 2

Accepted Solution

by:
MCM earned 100 total points
ID: 6388850
<FORM target="[THISPAGE.asp">
<SELECT NAME="CLIENT" OnChange="this.form.submit()">
<%
Dim cn
Dim adoRSC
Dim stSQLC
Dim strClient
Dim intClientID

Set cn = Server.CreateObject("ADODB.Connection")
Set adoRSC = Server.CreateObject("ADODB.Recordset")

stSQLC = "Select [CUSTOMER_ID],[NAME] from CUSTOMERS WHERE [NAME] IS NOT NULL order by [NAME] asc"

'Open a connection; the string referes to the DSN
cn.ConnectionString = "DRIVER=SQL Server" &_
   ";SERVER=SOCKS" & _
   ";UID=sa" & _
   ";PWD=moses" & _
   ";DATABASE=FINSYS"
cn.Open

' Associate the Recordset with the open connection
adoRSC.ActiveConnection = cn
adoRSC.Open stSQLC,cn

adoRSC.MoveFirst
 
Do While Not adoRSC.EOF
   
 intClientID = Server.HTMLEncode(adoRSC.Fields("CUSTOMER_ID"))
 strClient = Server.HTMLEncode(Trim(adoRSC.Fields("NAME")))%>
 
 <OPTION VALUE ="<%=intClientID%>"><%=strClient%>
 
 <%adoRSC.MoveNext

Loop%>

</SELECT>

<span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </span>

<% if len(Request("ClientID")>0 then %>
     <SELECT id=select1 name="USER">
     <%=retreiveUsers%>
     </SELECT></h2>
<%end if%>
</FORM>

<%
Function RetrieveUsers()
Dim adoRSU
Dim stSQLU
Dim strUser
Dim intUserID

Set adoRSU = Server.CreateObject("ADODB.Recordset")

stSQLU = "Select [USER_ID],[USER_NAME] from USERS WHERE [USER_NAME] IS NOT NULL order by [USER_NAME]
asc"

' Associate the Recordset with the open connection
adoRSU.ActiveConnection = cn
adoRSU.Open stSQLU,cn

adoRSU.MoveFirst
 
Do While Not adoRSU.EOF
   
 intUserID = Server.HTMLEncode(adoRSC.Fields("USER_ID"))
 strUser = Server.HTMLEncode(Trim(adoRSC.Fields("USER_NAME")))%>
 
 <OPTION VALUE ="<%=intUserID%>"><%=strUser%>
 
 <%adoRSC.MoveNext

Loop

End Function%>

0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 6388852
MCM
Is there a way that you can get a client event such as onChange to kick off a server side script
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:MCM
ID: 6388880
you see how it works? the onChange event submits the form back to the server, including ClientID. The server then realizes that a clientID has been identified, and only at that point does it generate output a list of users. you can't just call a server side script from the client;

this is obvious when you understand how ASP works, but for me as for every other developer who moved to ASP from some other medium, its a hard lesson to learn at first.

oops --

i see I wrote this:

if len(Request("ClientID")>0 then

when i should have written this:

if len(Request("Client")>0 then

the "CLIENT" here must correspond to the NAME of the SELECT that the user selected the client from.
0
 
LVL 2

Expert Comment

by:MCM
ID: 6388914
there are ways, but they are not good. Remote Scripting is a microsoft technology that uses a java applet to run back to the server and get new information; i don't beleive any one really uses this tech and it is certainly not in fashion -- MS may not even produce/support it any more.

You can write your own ActiveX control or java applet that will retrieve information from the server, but like remote scripting this is not really using ASP. under the ASP engine, what happens is:

-Server process server side scripts
-Server sends html to client, with no server side code included
-Client renders HTML, processes client side scripts.
-User does something that makes the client send an HTTP request back to the server for a whole other page

you don't get to instruct the client to send HTTP requests to your server for anything but a whole page at a time. retrieveUsers() is a function call, and HTTP doesn't know anything about function calls. it knows about page requests. web browsers make HTTP requests for pages, so getting them to get different kinds of data is very tricky, and always is working around the design of what browsers are designed to do. if you make your own activex/java solution, you are making a different client that interacts with you server in a different way than the browser.



0
 
LVL 2

Expert Comment

by:MCM
ID: 6388938
hope that was clear, my typing and thinking both seem a little defective today.

an option you can consider is to send all userid/name information to the browser in a javascript script. when the user clicks on "client", you run a client-side script that picks up the correct elements of the array, and dynamically loads them into the select. i could show you such a script, but i have to run; perhaps someone else will oblige if you are interested in such a thing. it won't work on all browsers, but it will work on most. but don't confuse this with the server and client interacting; you are just supplying the client with all possible answers to the user's question; when the user makes a selection, the client already has a list of users ready to supply. pages can get very big this way, if you have a ton of users, or if you want to go more than SELECT deep.
0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 6388967
Thanks a million for all the feedback MCM - I am now 80% there using your initial solution - just had to twig a few things(I am new to ASP so still not up to speed with all te concepts)

My dropdown list is working now but when it submits new browser window opens with the Client dropdown list reset to the first client but the users dropdown list is populated correctly showing only users for selected client.

How can I just get current page to refresh/resubmit not in new browser window and maintain the 'client' that was initially selected in client dropdown list.

Am I specifying the form target correctly

My script now looks like this:
*******************************************************
<form target="UserAppEntDev.asp"></o:p></o:p></SPAN></U>
<H1><U><SPAN style="COLOR: #ccffcc"></SPAN></U>&nbsp;</H1>
<H1><U><SPAN style="COLOR: #ccffcc">Specify Client/User Search Criteria<o:p></o:p></SPAN></U></H1>

<p class=MsoNormal><![if !supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></p>

<h2>Client<span style="mso-spacerun: yes">&nbsp;&nbsp; </span><span style="mso-spacerun:
 yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:
 yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>User</h2>

<h2><SELECT NAME="CLIENT" OnChange="this.form.submit()">
<%
Dim cn
Dim adoRSC
Dim stSQLC
Dim strClient
Dim intClientID

Set cn = Server.CreateObject("ADODB.Connection")
Set adoRSC = Server.CreateObject("ADODB.Recordset")

stSQLC = "Select [CUSTOMER_ID],[NAME] from CUSTOMERS WHERE [NAME] IS NOT NULL order by [NAME] asc"

'Open a connection; the string referes to the DSN
cn.ConnectionString = "DRIVER=SQL Server" &_
    ";SERVER=SOCKS" & _
    ";UID=sa" & _
    ";PWD=moses" & _
    ";DATABASE=FINSYS"
cn.Open

' Associate the Recordset with the open connection
adoRSC.ActiveConnection = cn
adoRSC.Open stSQLC,cn

adoRSC.MoveFirst
 
Do While Not adoRSC.EOF
   
  intClientID = Server.HTMLEncode(adoRSC.Fields("CUSTOMER_ID"))
  strClient = Server.HTMLEncode(Trim(adoRSC.Fields("NAME")))%>
 
  <OPTION VALUE ="<%=intClientID%>"><%=strClient%>
 
  <%adoRSC.MoveNext

Loop%>

</SELECT><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </span>

<SELECT id=select1 name="USER">

<% if len(Request("CLIENT"))>0 then
    RetrieveUsers(Request("CLIENT"))%>
    </SELECT></h2>
<%end if%>
</FORM>

<%
Function RetrieveUsers(intClientID)
Dim adoRSU
Dim stSQLU
Dim strUser
Dim intUserID

Set adoRSU = Server.CreateObject("ADODB.Recordset")

stSQLU = "Select [USER_ID],[USER_NAME] from USERS WHERE [USER_NAME] IS NOT NULL AND [CUSTOMER_ID] = " & intClientID & " order by [USER_NAME] asc"

' Associate the Recordset with the open connection
adoRSU.ActiveConnection = cn
adoRSU.Open stSQLU,cn

adoRSU.MoveFirst
 
Do While Not adoRSU.EOF
   
  intUserID = Server.HTMLEncode(adoRSU.Fields("USER_ID"))
  strUser = Server.HTMLEncode(Trim(adoRSU.Fields("USER_NAME")))%>
 
  <OPTION VALUE ="<%=intUserID%>"><%=strUser%>
 
  <%adoRSU.MoveNext

Loop

End Function%>
***********************************************************
0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 6388975
Yes my thinking etc. is a bit down today as well MCM - socialising till late last night - not firing an all cylinders today

0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 6388984
Yes my thinking etc. is a bit down today as well MCM - socialising till late last night - not firing an all cylinders today

0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 6389196
Hi MCM
The reason it was opening in new window was because of the target parameter - I did not fully understand this setting.
<FORM target="UserAppEntDev.asp"

I just removed this target setting and now ASP page works just simply refreshing current page and showing only users for selected client.

The only other thing I have to do is to maintain client list box at the client that was initially selected because when you refresh it resets to the first entry.
If you have any ideas for this it would be appreciated

Cheers


B Cunney


0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 6389201
Cheers
MCM
0
 
LVL 2

Expert Comment

by:MCM
ID: 6391203
howdy --
i was off duty this afternoon, sorry to abandon you. good job removing target; i know it's counterintuitive, but what you really want is "ACTION="[current page name]" and it is safer to specify it than to omit it.


when you loop through your list, which goes in your original code:

<%
Do While Not adoRSC.EOF
 
intClientID = Server.HTMLEncode(adoRSC.Fields("CUSTOMER_ID"))
strClient = Server.HTMLEncode(Trim(adoRSC.Fields("NAME")))%>

<OPTION VALUE ="<%=intClientID%>"><%=strClient%>

<%adoRSC.MoveNext

Loop
%>

draw the option value thusly:

if intClientID=Request("CLIENT") then
    <OPTION VALUE ="<%=intClientID%>" SELECTED><%=strClient%>
else
    <OPTION VALUE ="<%=intClientID%>" ><%=strClient%>
end if

and you will see the originaly choice pre-selected in your SELECT. good luck.



0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 6391738
Thanks a million for this last piece of code MCM has worked a dream - I have change the <FORM ACTION to current page as well.


Cheers


B Cunney
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question