ASP and Javascript problem

peterdoherty

Well-known member
Joined
Feb 24, 2003
Messages
49
Hi there I am trying to use the following javascript function with ASP code in it . The problem is that the ASP variable j never gets incremented. Can anyone spot the problem? Is there anyway that I can use the javascript variable i in the aspcode (the lstGroupMembers.Items(j).Text()) to select the next item in the list box each time the loop is iterated ?

Help will be greatly appreciated.

Peter

Code:
<script language="javascript">		
			function ReturnGroup()
				{
				<% Dim j
					Dim sName
					Dim x 
					j=0 %>
					//Loop around and all all the groups members to the selected text box
					var i;
					for (i=0; i< <%=lstGroupMembers.Items.Count()%>; i++)
						{
						//Get just the email address from the list
						<% 
						x = Right(lstGroupMembers.Items(j).Text(), len(lstGroupMembers.Items(j).Text())- InStr(lstGroupMembers.Items(j).Text(), "("))
						sName = Left(x, (Len(x)-1))
						%>
						window.opener.document.forms[GroupEMail].elements[<%=Page.Request.QueryString("Caller")%>].value = window.opener.document.forms[GroupEMail].elements[<%=Page.Request.QueryString("Caller")%>].value  + <%= sName %> + <%=j%>, ;
						<% j=j+1 %> // THIS SEEMS TO BE ONLY EXECUTED ONCE NO MATTER HOW MANY TIMES THE JAVA LOOP IS ITERATED
						}
				}
		</script>
 
ok ive never coded in aspx, but from what i read, i think every time you %> all your variables are fried, u have to specify them to be available for the entire site, or, for the entire user or soemthing.

there was a post about keeping variables alive for pop ups, try to find that.
 
You have to know ASP is server site language and javascript is client site language. ASP code will run first in webserver before javascript code, so, I dont think you code is workable...
 
Thanks guys. I think I may have to put the listbox items into an array and then pass this array into the javascript function. Does any one know how I could do this? i.e. pass a VB array inot a javascript function?

Cheers
 
I dont understand! Javascript can direct handle client site listbox, why you use server site control then?
 
I need to use a SS Listbox because there are some db operations on some of the listbox events. The lisbox contents is controlled by a combo box selection also so the easiest way to do tis was as SS controls.


I think the best thing to do would be pass an array into the javascriipt function. This array needs to be populated in VB script though in the VB back end code of the page. Does anyone know how to pass this vb array into a Javascript function?
 
You mean every time user select the combobox, your page will submit back to server and read from database, then display items in listbox?

What do you think if I put my needed data from database to javascript array on page load in client site, so if user select item from combobox, I just read the needed data from javascript array and display in listbox (all done in client site).

Do you think this is better? or I misunderstand your situation?
 
Yep youve got it in one thets what Im trying to do. This site is only gonna be based on an internal intranet so performance is not an issue thats why its done like this.

I like your suggestion. It sounds like an excellent solution ot the problem but how would I go about doing this? I dont know how to put the data into a javascript array????

I take it then this would mean the 2 controls would be html controls rather than asp controls.
 
You need to put your Javascript in a hidden textbox. You can direct put this array in your javascript function, but it will cause you error if the array too long (my experience)

My array name is "arrMenu" in this example

<INPUT TYPE=hidden NAME=hArr VALUE=arrMenu = new Array(
new Array(new Array("AAA_code","AAA_description")),
new Array(new Array("BBB_code","BBB Description"),new Array("CCC_code","CCC Description"))>

In this Array, when user select first item in first combobox, then the second listbox will display "AAA_description" (only 1 item in this example); if user select the second item in first combobox, "BBB description" and "CCC description" (2 items) will display in select listbox

Do you understand what I mean?

function showMenuItems() {
var form = document.Form1;
eval(form.hArr.value);

for (j=form.lstItem.length; j>=0; j--) {
form.lstItem[j] = null;
}
for (i=0; i<arrMenu[form.cboMod.selectedIndex].length; i++) {
form.lstItem.options[form.lstItem.length] = new Option(arrMenu[form.cboMod.selectedIndex][1],arrMenu[form.cboMod.selectedIndex][0]);
}

if (form.lstItem.length > 0) {
form.lstItem.selectedIndex = 0;
}
}


In this example javascript code, "cboMod" is my first combobox and "lstItem" is my second listbox
 
Wll i have chnged the design so the cbo items are aded to the parent form. Then when the actual list members are needed in the parent form they are resolved from the db.
 
Back
Top