Connecting Tech Pros Worldwide Forums | Help | Site Map

JavaScript form within ASP.NET webpage

Newbie
 
Join Date: Sep 2008
Posts: 2
#1: Sep 25 '08
I am converting a site from classic ASP to ASP.NET (2.0).

One page has the following JS form:
Expand|Select|Wrap|Line Numbers
  1. <body>
  2.  
  3. <script language="JavaScript">
  4.  var Makes0Array = new Array(new Array('0','Select model   '));
  5.  var Makes1Array = new Array(new Array('3','145'),new Array('11','Spider'));
  6.  var Makes2Array = new Array(new Array('12','A2'),new Array('311','A3'));
  7.  
  8.  function SwitchMakes() {
  9.     var Makes = document.searchCriteria.CarMakes.options[document.searchCriteria.CarMakes.selectedIndex].value;
  10.     var modelindex = document.searchCriteria.CarModels.selectedIndex;
  11.     var length = eval('Makes'+Makes+'Array.length');
  12.  
  13.     for (i = document.searchCriteria.CarModels.length-1; i >= 0; i--)
  14.         {document.searchCriteria.CarModels.options[i] = null;}
  15.  
  16.     var numberOfElements = 0;
  17.  
  18.     for (var x = 0; x < length; x++)
  19.         {var newoption = new Option(eval('Makes'+Makes+'Array[x][1]'), eval('Makes'+Makes+'Array[x][0]'), true, true);
  20.         document.searchCriteria.CarModels.options[document.searchCriteria.CarModels.length] = newoption;
  21.         numberOfElements++;}
  22.  
  23.     if ((modelindex >= 0) && (modelindex < numberOfElements))
  24.         {document.searchCriteria.CarModels.selectedIndex = modelindex;}
  25. }
  26.  
  27. function submitForm()
  28.   {document.searchCriteria.submit();}
  29. </script>
  30.  
  31. <form method="get" action="show_model.asp" name="searchCriteria">
  32.     <p>Make</p>
  33.     <select name="CarMakes" onchange="SwitchMakes()">
  34.     <option value="0">Select make</option>
  35.     <option value="1">Alfa Romeo</option>
  36.     <option value="2">Audi</option>
  37.     </select>
  38.  
  39.     <p>Model</p>
  40.     <select name="CarModels"></select>
  41.  
  42.     <a href="javascript:submitForm()"><input type="submit" value="go" /></a>
  43. </form>
  44.  
  45. </body>
Of course, as soon as this code is placed within the ASP.NET <FORM> tags the code will no longer work. As a beginner I am a loss as to how to get it to work again.

Please help!

DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#2: Sep 26 '08

re: JavaScript form within ASP.NET webpage


Hi misterdavid,

Welcome to Bytes.com! I hope you find the site useful. Please don't forget to wrap your code in CODE tags - it makes your posts much easier to read - and please read the Posting Guidelines if you have not done so already. Thanks.

What is it that this javascript is trying to achieve?

Dr B
Newbie
 
Join Date: Sep 2008
Posts: 2
#3: Sep 26 '08

re: JavaScript form within ASP.NET webpage


Hi DrB, thanks for responding to my question.

The javascript is tried and tested (it produces two drop down lists, the contents of the second depending on the selection made from the first) when incorporated in a classic ASP page.

The only problem I am having is getting the script to work within an ASP.NET type page.

As soon as the javascript form is inseted between the ASP.NET form tags, it fails.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,131
#4: Sep 26 '08

re: JavaScript form within ASP.NET webpage


Quote:

Originally Posted by misterdavid

Hi DrB, thanks for responding to my question.

The javascript is tried and tested (it produces two drop down lists, the contents of the second depending on the selection made from the first) when incorporated in a classic ASP page.

The only problem I am having is getting the script to work within an ASP.NET type page.

As soon as the javascript form is inseted between the ASP.NET form tags, it fails.

You cannot use JavaScript to populate ASP.NET DropDownList controls.
This is because the server checks to make sure that the controls have not been modified...since the JavaScript modifies what is listed in the it will fail validation.

You should create and populate your DropDownList in your sever side code instead of using JavaScript.

-Frinny
Reply