Options added to select list with JavaScript lost on postback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mario Vargas

    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:

    myOpenerListCtr l.options[myOpenerListCtr l.options.lengt h] = new Option(
    districtName, districtID );

    I also tried using standard DOM syntax:

    var newOption = document.create Element( "option" );
    newOption.setAt tribute( "value", districtID );
    newOption.inner Text = districtName;
    myOpenerListCtr l.appendChild( newOption )

    But neither is kept in the postback.

    Thanks!

    Mario


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

    #2
    Re: Options added to select list with JavaScript lost on postback

    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



    "Mario Vargas" <mariovargas@th ecourier.comwro te in message
    news:eR1DfYEtHH A.508@TK2MSFTNG P02.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:
    >
    myOpenerListCtr l.options[myOpenerListCtr l.options.lengt h] = new Option(
    districtName, districtID );
    >
    I also tried using standard DOM syntax:
    >
    var newOption = document.create Element( "option" );
    newOption.setAt tribute( "value", districtID );
    newOption.inner Text = districtName;
    myOpenerListCtr l.appendChild( newOption )
    >
    But neither is kept in the postback.
    >
    Thanks!
    >
    Mario
    >

    Comment

    • Mario Vargas

      #3
      Re: Options added to select list with JavaScript lost on postback

      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.netw rote in
      message news:5D2AC4B3-D24B-41C4-85D0-36A429E94F0F@mi crosoft.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

      >
      >
      "Mario Vargas" <mariovargas@th ecourier.comwro te in message
      news:eR1DfYEtHH A.508@TK2MSFTNG P02.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:
      >>
      >myOpenerListCt rl.options[myOpenerListCtr l.options.lengt h] = new Option(
      >districtName , districtID );
      >>
      >I also tried using standard DOM syntax:
      >>
      >var newOption = document.create Element( "option" );
      >newOption.setA ttribute( "value", districtID );
      >newOption.inne rText = districtName;
      >myOpenerListCt rl.appendChild( newOption )
      >>
      >But neither is kept in the postback.
      >>
      >Thanks!
      >>
      >Mario
      >>
      >

      Comment

      • Laurent Bugnion, MVP

        #4
        Re: Options added to select list with JavaScript lost on postback

        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

        Comment

        • Mario Vargas

          #5
          Re: Options added to select list with JavaScript lost on postback

          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.chwr ote in message
          news:uu4aA3JtHH A.4916@TK2MSFTN GP05.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

          Comment

          Working...