468,784 Members | 1,564 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,784 developers. It's quick & easy.

Options added to select list with JavaScript lost on postback

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
Jun 21 '07 #1
4 2866
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" <ma*********@thecourier.comwrote in message
news:eR*************@TK2MSFTNGP02.phx.gbl...
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
Jun 21 '07 #2
Thanks, Steve! I also thought about this approach, but was hoping for a
better way...

Mario

"Steve C. Orr [MCSD, MVP, CSM, ASP Insider]" <St***@Orr.netwrote in
message news:5D**********************************@microsof t.com...
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" <ma*********@thecourier.comwrote in message
news:eR*************@TK2MSFTNGP02.phx.gbl...
>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

Jun 21 '07 #3
Hi,

Mario Vargas wrote:
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
Jun 22 '07 #4
Hi Laurent,

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

Mario

"Laurent Bugnion, MVP" <ga*********@bluewin.chwrote in message
news:uu**************@TK2MSFTNGP05.phx.gbl...
Hi,

Mario Vargas wrote:
>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

Jun 22 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Mark Fox | last post: by
2 posts views Thread by kiranmn75 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.