This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes ASP with JavaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "ASP with JavaScript" Watch "ASP with JavaScript" New topic
Author

ASP with JavaScript

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I want to include some asp code ( that will get data from the database ) in my JavaScript function, for example I have two dropdown boxes one for brand and the other for category, when the user selects a brand I want to modify the category dropdown box, so it should only include the category's under that brand which the user selected in the brand dropdown box, if anyone could help please do so, I will greatly appreciate it.
Yoel Stern
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I will post my function so you will have a look at my code, and tell me whats wrong !
swaroop shastri
Ranch Hand

Joined: Aug 15, 2001
Posts: 65
Looks like you are combining server side with client side code!
There are many alternatives to doing it.
You might want to refresh the page and pass the combo selected as a parameter and populate the appropriate combo box with the values you might want. Javascript Source is a nice place to search on JS topics JS code.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Swaroop Shastri,

I was considering to do it your way ( to refresh the page and pass the combo selected as a parameter and populate the appropriate combo box with the values you might want ), I would like to know if there is another way without refreshing the page.
And the problem with refreshing the page is that it reloads with out any changes to the page except to the combo box, and the user will be wondering what exactly changed on the page, and why it was reloaded.
Yoel Stern
swaroop shastri
Ranch Hand

Joined: Aug 15, 2001
Posts: 65
Here is a possible solution to your problem.
You can have a hidden object, lets say hdCategory.
On the server side u will have to run the following query.
"SELECT category, brand FROM products "
Populate the hidden object as a name value pair.
For ex :
<input type=hidden name="hdCategory" value="cat1~brnd1~~cat2~brnd1~~cat3~brnd2"> and so on.
Now when the brand combo changes use this hidden value to get the categories for that brand and populate the other combo box
Feel free to contact me if i can be of further assistance.
[ February 13, 2002: Message edited by: swaroop shastri ]
swaroop shastri
Ranch Hand

Joined: Aug 15, 2001
Posts: 65
This method will populate the shift combo based on what the plant combo is. I wrote this long time(4 yrs!) back for a project i was working on with exactly the requirements you have. I had it in my backup. I hope it still works. It was in the ie3/netscape3 era!

hdPlantShift is akin to the hidden object i talked about in my prev post. Also remember to set the value of this hidden object to "" just before submitting the form. Some browsers and web servers have a problem accepting large values for form fields.
A bit of tweaking here and there might get it working for you.
Hope it helps!
function combos()
{
var colsep = "~", rowsep = "~~";
var rows = new Array();
//the hdPlantShift object contains the plant shift values
var str = document.pgVTSShiftHolidayMap.hdPlantShift.value;

for (i =0 ; i < document.pgVTSShiftHolidayMap.cbPlant.length ; i++)
{
for(j = document.pgVTSShiftHolidayMap.cbPlant.length - 1 ; j > i ; j--)
{
if(document.pgVTSShiftHolidayMap.cbPlant.options[j].value == document.pgVTSShiftHolidayMap.cbPlant.options[i].value )
{ // remove duplicates in plant combo
document.pgVTSShiftHolidayMap.cbPlant.options[j] = null;
}
}
}


if(str == "") return true;
rows = str.split(rowsep);

var i = 0;
var k = 0;

var selectedIndex = document.pgVTSShiftHolidayMap.cbPlant.selectedIndex;
var selectedIndexValue = document.pgVTSShiftHolidayMap.cbPlant[selectedIndex].value ;

var length = document.pgVTSShiftHolidayMap.cbShift.length;

for (i =0 ; i<length ; i++)
{
document.pgVTSShiftHolidayMap.cbShift.options[k] = null;
}

for (i =0 ; i< rows.length ; i++)
{
var cols = new Array();
cols = rows[i].split(colsep);
if(selectedIndexValue == cols[0])
{
document.pgVTSShiftHolidayMap.cbShift.options[k] = new Option(cols[3], cols[2])
k++;
}
} // end for
document.pgVTSShiftHolidayMap.cbShift.selectedIndex = 0;
document.pgVTSShiftHolidayMap.cbPlant.selectedIndex = selectedIndex;

return true;
}
[ February 13, 2002: Message edited by: swaroop shastri ]
[ February 13, 2002: Message edited by: swaroop shastri ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The reason that your page isn't working is order of execution. ASP gets parsed by ASP.dll first before anything else happens. Then the server builds an HTTP response and sends it back to the browser. Because that is the order of things, you can not execute ASP code as part of an event handler. It just won't work.
If you need to make the event trigger an ASP function, you will have to submit the page, or find some other solution. A really common development technique it to have every page submit to its self. You name all of your submit buttons the same thing and then you check the value at the top of the page.

At the top of your page:

An alternative solution is to have your ASP write your javascript function. This works well if you need to get a value from your ASP page for use in client script, but that value is not likely to change while the page is loaded. For example, if I wanted to pop and alert dialog box with my customer's name, I could do something like this:

HTH
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi both of you,
Sorry that I didn't let you know how I solved my problem, since I didn't get a chance to try it out as soon as I do I will let you know.
Thanks
Yoel
 
jQuery in Action, 2nd edition
 
subject: ASP with JavaScript
 
Similar Threads
Display the Selected DropDown Value to another JSP Page
dropdownboxes as input field ???
Retaining javascript values in jsp
extracting some elements from ArrayList on JSP
how to get value selected in dropdown without using javascript and before form submission