Options added to select list with JavaScript lost on postback

M

Mario Vargas

Hello all,

I am trying to dynamically add elements to a select list (which is running
on the server, runat="server") with JavaScript, but when I postback, the new
elements are lost. I'm not sure how to tell ASP.Net to post back these new
elements as well. Any ideas or sources where you could point me to will be
greatly appreciated. I am still using ASP.Net 1.1.

This is part of the code I am using to dynamically add the element:

myOpenerListCtrl.options[myOpenerListCtrl.options.length] = new Option(
districtName, districtID );

I also tried using standard DOM syntax:

var newOption = document.createElement( "option" );
newOption.setAttribute( "value", districtID );
newOption.innerText = districtName;
myOpenerListCtrl.appendChild( newOption )

But neither is kept in the postback.

Thanks!

Mario
 
S

Steve C. Orr [MCSD, MVP, CSM, ASP Insider]

Most people add such data to a hidden form field, which you would then
manually process upon postback to keep the lists in sync on both ends.
 
M

Mario Vargas

Thanks, Steve! I also thought about this approach, but was hoping for a
better way...

Mario

Steve C. Orr said:
Most people add such data to a hidden form field, which you would then
manually process upon postback to keep the lists in sync on both ends.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net


Mario Vargas said:
Hello all,

I am trying to dynamically add elements to a select list (which is
running on the server, runat="server") with JavaScript, but when I
postback, the new elements are lost. I'm not sure how to tell ASP.Net to
post back these new elements as well. Any ideas or sources where you
could point me to will be greatly appreciated. I am still using ASP.Net
1.1.

This is part of the code I am using to dynamically add the element:

myOpenerListCtrl.options[myOpenerListCtrl.options.length] = new Option(
districtName, districtID );

I also tried using standard DOM syntax:

var newOption = document.createElement( "option" );
newOption.setAttribute( "value", districtID );
newOption.innerText = districtName;
myOpenerListCtrl.appendChild( newOption )

But neither is kept in the postback.

Thanks!

Mario
 
L

Laurent Bugnion, MVP

Hi,

Mario said:
Thanks, Steve! I also thought about this approach, but was hoping for a
better way...

Mario

To be fair, it's not ASP.NET which is guilty here, but the fact that the
items of a SELECT element are not transmitted on postback, but only the
selected value. Which makes sense, if you think of it, or else the
requests could be huge (think of these SELECT with the list of all
countries, for example).

Greetings,
Laurent
 
M

Mario Vargas

Hi Laurent,

That's a very good point and something I didn't know about. Thanks for
sharing!

Mario

Laurent Bugnion said:
Hi,

Mario said:
Thanks, Steve! I also thought about this approach, but was hoping for a
better way...

Mario

To be fair, it's not ASP.NET which is guilty here, but the fact that the
items of a SELECT element are not transmitted on postback, but only the
selected value. Which makes sense, if you think of it, or else the
requests could be huge (think of these SELECT with the list of all
countries, for example).

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

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,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top