Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cannot retain value in listbox.

    I have a listbox that is populated dynamically from an ADO recordset. I only show 10 rows at a time in the frame. I default to the first 10 and when say 41-50 is selected the page reloads in the frame and the correct rows are populated, however my list box reverts to 1-10. How can I default the listbox to the selection that was made?

    By the way the listbox is in the page that gets reloaded.

    Here is the code that builds the box.

    var lowVal;
    var highVal;
    ct= <%=ct%>;

    if (ct >= 10) {
    var quotient= parseInt(ct) / 10;
    var remainder= parseInt(ct) % 10;

    for(var i=1;i<=quotient;i++){
    lowVal = (i-1) * 10 + 1;
    highVal = i * 10;
    document.write("<option>" + lowVal + " - " + highVal);
    }
    if (remainder != 0){
    lowVal= ((i-1) * 10) + 1;
    highVal= ((i-1) * 10) + remainder;
    document.write("<option>" + lowVal + " - " + highVal);
    }

    iniVal=document.getElementById("criSel").options[0].text;
    iniLowVal=iniVal.substring(0,iniVal.indexOf("-"));
    iniHighVal=iniVal.substring(iniVal.indexOf("-")+1,iniVal.length);

    }else if(ct < 10) {
    document.getElementById("criSel").style.display="none";
    document.getElementById("SelCriLbl").style.display="none";
    }

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Here's the generic code I use for keeping the same dropdown value with ASP.

    edit: either add a value to your <option> tags or change my script example from .value to .text (I think).

    Code:
    <html>
    <head>
    <script language="javascript">
    function set_dropdown(){
     for (var i=0; i<document.myform.mydropdown.length; i++)
       if (document.myform.mydropdown.options[i].value=='<%=request.querystring("mydropdown")%>')
         document.myform.mydropdown.options[i].selected=true;
    }
    </script>
    </head>
    ...
    ...
    
    <body onload="set_dropdown();">
    your form
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks!

    Thanks for the help!!!

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    arnyinc, you should read my javascript tips

    This makes for a much more efficient function
    Code:
    <html>
    <head>
    <script language="javascript">
    function set_dropdown( selObj, val )
    {
        for ( var opt, i=0; ( opt = selObj.options[i] ); i++ )
            if ( opt.value == val )
                opt.selected = true;
    }
    </script>
    </head>
    ...
    ...
    
    <body onload="set_dropdown( document.myform.mydropdown, '<%=request.querystring("mydropdown")%>' )">
    your form
    </body>
    </html>
    Now the same function can be used to set any number of SELECT elements
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    That's completely unnecessary when there is only one select box. I suppose if there were multiple select boxes your example is much better and more generalized.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Even then, creating a reference is still preferred
    Code:
    function set_dropdown() 
    {
        var selObj = document.myForm.mydropdown;
        for ( var opt, i=0; ( opt = selObj.options[i] ); i++ )
            if ( opt.value == '<%=request.querystring("mydropdown")%>' )
                opt.selected = true;
    }
    Please note: I'm speaking about JS in general, and not just harping on this function
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •