• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ASP with JavaScript

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will post my function so you will have a look at my code, and tell me whats wrong !
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic