Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

keeping dropdown value on postback

Status
Not open for further replies.

arkadia93

Programmer
Oct 19, 2006
110
GB
How do you do the equivalent of the code below (to keep the list item selected after a postback), when you are dynamically populating a dropdown with Javascript (see code 2)?

Code 1 :

<select name="Brand" onChange="changeBrand(this);" ID="Brand">
<option value="all" <%if request("Brand")="all" then response.Write "selected" end if%>>all</option>
<option value="Equanet" <%if request("Brand")="Equanet" then response.Write "selected" end if%>>Equanet</option>
<option value="PC World Business" <%if request("Brand")="PC World Business" then response.Write "selected" end if%>>PC World Business</option>
<option value="PC World Education" <%if request("Brand")="PC World Education" then response.Write "selected" end if%>>PC World Education</option>
<option value="Business Flag" <%if request("Brand")="Business Flag" then response.Write "selected" end if%>>Business Flag</option>
<option value="Cold" <%if request("Brand")="Cold" then response.Write "selected" end if%>>Cold</option>
</select>

Code 2 :

function changeBrand(elem)
{
var j, Brand;
var shiftText;

if(elem.selectedIndex == 0) //if they selected [select a day] stmt
return false; //do nothing and leave quietly

//Clear the second drop down of all but top [select a shift]
for (i = document.form1.LoadID.options.length - 1; i >= 1; i--) document.form1.LoadID.options = null;
document.form1.LoadID.selectedIndex = 0;

//add to list box
if (document.form1.Brand.selectedIndex == 1)
{
document.form1.LoadID.options[1] = new Option('Account', 'Account');
document.form1.LoadID.options[2] = new Option('Education', 'Education');
}
else if (document.form1.Brand.selectedIndex == 2)
{
document.form1.LoadID.options[1] = new Option('Account', 'Account');
}
else if (document.form1.Brand.selectedIndex == 3)
{
document.form1.LoadID.options[1] = new Option('Education', 'Education');
}
else if (document.form1.Brand.selectedIndex == 4)
{
document.form1.LoadID.options[1] = new Option('Business Flag', 'Business Flag');
}
else if (document.form1.Brand.selectedIndex == 5)
{
document.form1.LoadID.options[1] = new Option('Cold', 'Cold');
}
}
 
If you're repopulating the dropdown with a new list of options, which one is selected?
 
Ah, sorry, I think the answer to your question is that the selected option is always Request.Form("Brand") on postback.
 
If you needed it available in your javascript functions maybe you could add this:
Code:
var postedBrand;
postedBrand = <%=Request.Form("Brand")%>;
 
[tt] //add to list box
if (document.form1.Brand.selectedIndex == 1)
{
document.form1.LoadID.options[1] = new Option('Account', 'Account');
document.form1.LoadID.options[2] = new Option('Education', 'Education');
}
else if (document.form1.Brand.selectedIndex == 2)
{
document.form1.LoadID.options[1] = new Option('Account', 'Account');
[blue]document.form1.LoadID.options[1].selected=true;[/blue]
}
else if (document.form1.Brand.selectedIndex == 3)
{
document.form1.LoadID.options[1] = new Option('Education', 'Education');
[blue]document.form1.LoadID.options[1].selected=true;[/blue]
}
else if (document.form1.Brand.selectedIndex == 4)
{
document.form1.LoadID.options[1] = new Option('Business Flag', 'Business Flag');
[blue]document.form1.LoadID.options[1].selected=true;[/blue]
}
else if (document.form1.Brand.selectedIndex == 5)
{
document.form1.LoadID.options[1] = new Option('Cold', 'Cold');
[blue]document.form1.LoadID.options[1].selected=true;[/blue]
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top