changing options in an asp dropdownlist with javascript

Hi everyone,

I have an asp dropdownlist that has a number of options. I then change the options using javascript based on a user's choice in a previous field. Now my problem is when the page is posted back the options remain the way they were before the javascript changed them. Does anyone know how to change the options of an asp dropdownlist using javascript and have those changes reflected when the page is posted-back?
tdiscoverAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
agarawalamitConnect With a Mentor Commented:
You can store the changed value in a hidden box. And when the page is postedback and reload use the vales in the hidden box and reload the dropdown using javascript.
0
 
Pratima PharandeCommented:
check that ... if you are adding code to fill the dropdown in Pageload ..if yes then put that code inside ispostback condition

 if (!Page.IsPostBack)
        {

           //Code here

        }
0
 
tdiscoverAuthor Commented:
Thanks pratima_mcs but I don't have any code in my pageload event. Good idea to check though!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tdiscoverAuthor Commented:
Just in case you think there could be something wrong with my javascript, here is the html code for the dropdownlist before and after the javascript changes the options (I'm taking the code from view source in the browser).
Before:
<select name="ctl00$ContentPlaceHolder1$TE_Section" id="ctl00_ContentPlaceHolder1_TE_Section" style="width:160px;">
	<option value="">blah</option>
	<option value="">blah</option>
	<option value="">blah</option>
	<option value="">blah</option>
</select>
 
After:
<select name="ctl00$ContentPlaceHolder1$TE_Section" id="ctl00_ContentPlaceHolder1_TE_Section" style="width: 160px;">
<option value="Car">Car</option>
<option value="Campervan">Campervan</option>
</select>

Open in new window

0
 
Pratima PharandeCommented:

You need to check during postback did you get the changed value .....

means just Response.write(ddl.selectedvalue ) on pageload

and check whether you are getting changed value ..

if yes then in prerender event just assign that value again to drpdown
0
 
Pratima PharandeCommented:

can you post your javascript code here ?
0
 
Qaiser_Mehmood_MughalCommented:
If you will doing the filling code in
If(!IspostBack)
{
//code
}
then your problem will be solve, as viewstate mantain the state of controls during the postback calls.
0
 
tdiscoverAuthor Commented:
Thanks for all your comments everyone. Sorry for not replying sooner (it was night time here). Pratima_mcs, I don't get the value on postback because the page is not actually posting-back. It posts to a different page that I use "PreviousPage.getDDLvalue" where getDDLvalue is a property that returns DDL.SelectedValue. This method works for textboxes and the initial values of the DDL but not for the DDL after it has had it's options replaced using javascript.

I've attached the javascript. If you need anything else let me know.
//First I remove all the options from the dropDownList by calling the following function:
function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}
 
//Then I call the following function each time I want to add a new option to the dropDownList
 
function addOption(selectbox, text, value){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

Open in new window

0
 
tdiscoverAuthor Commented:
Thanks! After days of trying various ways your solution worked best.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.