ASP.NET / DropDownlist JavaScript Problem


I have an ASP.NET DropDownList server control which gets populated on the client side via JavaScript, like so:

var oOption = document.createElement('OPTION');
oOption.value = sText;
oOption.text = sText;
listbox.options.add(oOption);

When a postback occurs, this ListBox doesn't contain crap. It's empty.

I've tried checking it for contents in Page_Load() and in some of the handlers. Nowhere does it seem to arrive with the items I added through JavaScript.
LVL 2
prashanthanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

razoCommented:
try calling the javascript function that populates the dropdownlist from the page_load event
0
RejojohnyCommented:
listbox, dropdownlist etc does not maintain the items added on the clients-side when a postback occurs as the viewstate does not know of these items added from the client (on the browser)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brdrokCommented:
hmmm....dunno if that is possible via javascript but maybe add the newly added items into the url string (cookies maybe) and then extract that new data inside the page_load or whevever?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ThaloxCommented:

yes Rejojohny is right.
But you can access the value the user selected in the dropdownlist.

on client add option with javascript
var oOption = document.createElement('OPTION');
oOption.value = "TestVal";
oOption.text = "TestVal";
listbox.options.add(oOption);

and on server you can use
Request.Form["listbox"]
this will return the selected value ("TestVal" in this case)

hope that helps
Thalox
0
prashanthanAuthor Commented:
I tried to retrive the value through Request.Form("listbox"), but it always return the first item of the dropdownlist.

Is there any other way to acheive it.

0
RejojohnyCommented:
Request.Form("listbox") will always give you the selected value in the listbox and not the whole list of items ..
0
RejojohnyCommented:
Please do have alook at my comments ..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

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.