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!
MarioMost 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" <mariovargas@.thecourier.comwrote in message
news:eR1DfYEtHHA.508@.TK2MSFTNGP02.phx.gbl...
Quote:
Originally Posted by
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
>
Thanks, Steve! I also thought about this approach, but was hoping for a
better way...
Mario
"Steve C. Orr [MCSD, MVP, CSM, ASP Insider]" <Steve@.Orr.netwrote in
message news:5D2AC4B3-D24B-41C4-85D0-36A429E94F0F@.microsoft.com...
Quote:
Originally Posted by
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" <mariovargas@.thecourier.comwrote in message
news:eR1DfYEtHHA.508@.TK2MSFTNGP02.phx.gbl...
Quote:
Originally Posted by
>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
>>
>
Hi,
Mario Vargas wrote:
Quote:
Originally Posted by
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
Hi Laurent,
That's a very good point and something I didn't know about. Thanks for
sharing!
Mario
"Laurent Bugnion, MVP" <galasoft-lb@.bluewin.chwrote in message
news:uu4aA3JtHHA.4916@.TK2MSFTNGP05.phx.gbl...
Quote:
Originally Posted by
Hi,
>
Mario Vargas wrote:
Quote:
Originally Posted by
>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
0 comments:
Post a Comment