JavaScript form within ASP.NET webpage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • misterdavid
    New Member
    • Sep 2008
    • 2

    JavaScript form within ASP.NET webpage

    I am converting a site from classic ASP to ASP.NET (2.0).

    One page has the following JS form:
    Code:
    <body>
    
    <script language="JavaScript">
     var Makes0Array = new Array(new Array('0','Select model   '));
     var Makes1Array = new Array(new Array('3','145'),new Array('11','Spider'));
     var Makes2Array = new Array(new Array('12','A2'),new Array('311','A3'));
         
     function SwitchMakes() {
        var Makes = document.searchCriteria.CarMakes.options[document.searchCriteria.CarMakes.selectedIndex].value;
        var modelindex = document.searchCriteria.CarModels.selectedIndex;
        var length = eval('Makes'+Makes+'Array.length');
      
        for (i = document.searchCriteria.CarModels.length-1; i >= 0; i--)
            {document.searchCriteria.CarModels.options[i] = null;}
    
        var numberOfElements = 0;
      
        for (var x = 0; x < length; x++)
            {var newoption = new Option(eval('Makes'+Makes+'Array[x][1]'), eval('Makes'+Makes+'Array[x][0]'), true, true);
            document.searchCriteria.CarModels.options[document.searchCriteria.CarModels.length] = newoption;
            numberOfElements++;}
      
        if ((modelindex >= 0) && (modelindex < numberOfElements))
            {document.searchCriteria.CarModels.selectedIndex = modelindex;}
    }
    
    function submitForm()
      {document.searchCriteria.submit();}
    </script>
    
    <form method="get" action="show_model.asp" name="searchCriteria">
    	<p>Make</p>
    	<select name="CarMakes" onchange="SwitchMakes()">
    	<option value="0">Select make</option>
    	<option value="1">Alfa Romeo</option>
    	<option value="2">Audi</option>
    	</select>
    	
    	<p>Model</p>
    	<select name="CarModels"></select>
    	
    	<a href="javascript:submitForm()"><input type="submit" value="go" /></a>
    </form>
    
    </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
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    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

    Comment

    • misterdavid
      New Member
      • Sep 2008
      • 2

      #3
      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.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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...sinc e 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

        Comment

        Working...