Value and text are nothing on postback if ddl is filled client sid

C

Chris Lane

Hi,

Can somebody please explain why after postback a dropdownlist
selectedindex.value and text are nothing if the dropdownlist was populated
clientside with javascript.
Are there any workarounds besides using the request object to get the value
of the dropdownlist?

Thanks,

Chris
 
O

Oleg Kap

Hello Chris,
If you populate you drop down list on the client side, server side has no
knowledge of it.
It doesnt submit all values in the drop down list.
I cant think of any other way know to get those values other then using Request
object.
If you going to use this in many pages you might consider to write your own
component where client side will populate data and your component will submit
all values to the server side.

Hope it helps,
Oleg
 
P

Phillip Williams

Hi Chris,

Server controls that are composed on the server-side write a ViewState
encrypted on the rendered HTML page (if you right-mouse click on the page and
view the source of your HTML you will see an <input type="hidden"
name="__VIEWSTATE" > whose value contains the encrypted ViewState.

When you add an <option> to a dropdown list using JavaScript, you are not
adding equivalent values into the ViewState and therefore upon PostBack there
is no way for the ASP page to realize your changes.

A work around is to create <input type="hidden"> tags to manage your own
addition to the HTML, e.g.
<input type="hidden" id="AddedToMyDropDownList" value="Item1Val|Item1Text|
item2Val|Item2Text"> then on the server side while handling the Page.Load you
would process this value and add the new items to your dropdown list before
the method that would handle the SelectedIndexChanged event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top