...

View Full Version : Cannot retain value in listbox.



elcaro2k
01-14-2003, 12:50 PM
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";
}

arnyinc
01-14-2003, 03:13 PM
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).



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

elcaro2k
01-14-2003, 04:16 PM
Thanks for the help!!!

beetle
01-14-2003, 04:40 PM
arnyinc, you should read my javascript tips (http://www.sitepointforums.com/showthread.php?s=&threadid=90644) :D

This makes for a much more efficient function
<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 :D

arnyinc
01-14-2003, 05:17 PM
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.

beetle
01-14-2003, 05:23 PM
Even then, creating a reference is still preferred
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 ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum