Solved

changing options in an asp dropdownlist with javascript

Posted on 2008-06-11
9
556 Views
Last Modified: 2010-04-21
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?
0
Comment
Question by:tdiscover
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 21766499
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
 

Author Comment

by:tdiscover
ID: 21766560
Thanks pratima_mcs but I don't have any code in my pageload event. Good idea to check though!
0
 

Author Comment

by:tdiscover
ID: 21766601
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 21766615

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

Accepted Solution

by:
agarawalamit earned 500 total points
ID: 21766625
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
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 21766632

can you post your javascript code here ?
0
 
LVL 5

Expert Comment

by:Qaiser_Mehmood_Mughal
ID: 21766667
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
 

Author Comment

by:tdiscover
ID: 21774258
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
 

Author Closing Comment

by:tdiscover
ID: 31466449
Thanks! After days of trying various ways your solution worked best.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question