Listbox data going away

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

    Listbox data going away

    I have a .net app that a user currently enters a number in a text box,
    hits a button and a data call is executed. She wants the ability to
    enter in multiple numbers (up to 100).

    So to make things look better visually for that, I created a listbox
    under the text box. She enters the number in the text box, clicks
    another button I added and the number is stored in the list box. Then
    my plan was to grab all those numbers from the list box when she
    clicks the original button to do the data call.

    Here's my problem. I filled the listbox using java script - to
    prevent hitting the server every time the user clicks my "add"
    button. The listbox however is a server side lisbox. But, the moment
    I hit my original button (i.e."Run Report"), the data goes away in the
    listbox. I have tried to use the EnableViewState property and that
    doesn't work. I've tried using the AutoPostBack property and that
    doesn't work.

    I was told the issue is that the only thing retained in the list box
    is the item selected. So I tried to write a javascript method that
    would select all of them when the user clicks the "Run Report"
    button. It doesn't work, because I have to register the javascript
    code in my code behind and it clears that listbox before it executes
    even one line of code in the codebehind.

    I tried to create a hidden text box to just store the numbers too and
    that didn't work. I can't use a <inputwith a type of hidden cause
    that is a client control and I need this data on the server side. I
    tried to just create a asp text box and make it's visible property
    false, but then I can't access the text box on the client side when I
    want to fill it.

    I think I'm resigned to using a multi-line textbox to just let the
    user enter them in all at once, but I hate the idea because it leads
    the user down a path of potentially making many mistakes as he/she
    enters. I just can't believe that their is no way to keep this data.
    What am I missing?
  • Mark Rae [MVP]

    #2
    Re: Listbox data going away

    "Doogie" <dnlwhite@dtgne t.comwrote in message
    news:1031ffe4-a54c-4f3b-8409-b7fafd5b3658@k1 g2000prb.google groups.com...
    >I have a .NET app that a user currently enters a number in a text box,
    hits a button and a data call is executed. She wants the ability to
    enter in multiple numbers (up to 100).
    OK.
    So to make things look better visually for that, I created a listbox
    under the text box. She enters the number in the text box, clicks
    another button I added and the number is stored in the list box. Then
    my plan was to grab all those numbers from the list box when she
    clicks the original button to do the data call.
    Sounds reasonable.
    Here's my problem. I filled the listbox using JavaScript - to
    prevent hitting the server every time the user clicks my "add"
    button. The listbox however is a server side lisbox. But, the moment
    I hit my original button (i.e."Run Report"), the data goes away in the
    listbox. I have tried to use the EnableViewState property and that
    doesn't work. I've tried using the AutoPostBack property and that
    doesn't work.
    That's standard behaviour for ListBoxes - changes made to their elements
    collection via client-side JavaScript do not survive a postback.
    I was told the issue is that the only thing retained in the list box
    is the item selected. So I tried to write a JavaScript method that
    would select all of them when the user clicks the "Run Report"
    button. It doesn't work, because I have to register the JavaScript
    code in my code behind and it clears that listbox before it executes
    even one line of code in the codebehind.
    If your JavaScript is supposed to select all of the elements in a ListBox
    but, in fact, clears the ListBox, then there is either a bug in your
    JavaScript, or it's not running at the correct place in the page cycle. This
    would need to run in the OnClientClick event of the <asp:Button /control
    which does the postback.
    I tried to create a hidden text box to just store the numbers too and
    that didn't work.
    Can you be more specific?
    I can't use a <inputwith a type of hidden cause that is a client control
    and I need this data on the server side.
    Yes you can - just give it a runat="server" tag...
    I tried to just create a asp text box and make its visible property
    false, but then I can't access the text box on the client side when I
    want to fill it.
    That's because setting a webcontrol's Visible property to "true" prevents
    that control from being rendered to the client browser.
    I think I'm resigned to using a multi-line textbox to just let the
    user enter them in all at once, but I hate the idea because it leads
    the user down a path of potentially making many mistakes as he/she
    enters. I just can't believe that their is no way to keep this data.
    What am I missing?
    A hidden textbox is definitely the way to go here, so I'd suggest that your
    JavaScript is where the problem lies.

    Please show your JavaScript and how you call it.


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Doogie

      #3
      Re: Listbox data going away

      On Nov 7, 6:55 am, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
      "Doogie" <dnlwh...@dtgne t.comwrote in message
      >
      news:1031ffe4-a54c-4f3b-8409-b7fafd5b3658@k1 g2000prb.google groups.com...
      >
      I have a .NET app that a user currently enters a number in a text box,
      hits a button and a data call is executed.  She wants the ability to
      enter in multiple numbers (up to 100).
      >
      OK.
      >
      So to make things look better visually for that, I created a listbox
      under the text box.  She enters the number in the text box, clicks
      another button I added and the number is stored in the list box.  Then
      my plan was to grab all those numbers from the list box when she
      clicks the original button to do the data call.
      >
      Sounds reasonable.
      >
      Here's my problem.  I filled the listbox using JavaScript - to
      prevent hitting the server every time the user clicks my "add"
      button.  The listbox however is a server side lisbox.  But, the moment
      I hit my original button (i.e."Run Report"), the data goes away in the
      listbox.  I have tried to use the EnableViewState property and that
      doesn't work.  I've tried using the AutoPostBack property and that
      doesn't work.
      >
      That's standard behaviour for ListBoxes - changes made to their elements
      collection via client-side JavaScript do not survive a postback.
      >
      I was told the issue is that the only thing retained in the list box
      is the item selected.  So I tried to write a JavaScript method that
      would select all of them when the user clicks the "Run Report"
      button.  It doesn't work, because I have to register the JavaScript
      code in my code behind and it clears that listbox before it executes
      even one line of code in the codebehind.
      >
      If your JavaScript is supposed to select all of the elements in a ListBox
      but, in fact, clears the ListBox, then there is either a bug in your
      JavaScript, or it's not running at the correct place in the page cycle. This
      would need to run in the OnClientClick event of the <asp:Button /control
      which does the postback.
      >
      I tried to create a hidden text box to just store the numbers too and
      that didn't work.
      >
      Can you be more specific?
      >
      I can't use a <inputwith a type of hidden cause that is a client control
      and I need this data on the server side.
      >
      Yes you can - just give it a runat="server" tag...
      >
      I tried to just create a asp text box and make its visible property
      false, but then I can't access the text box on the client side when I
      want to fill it.
      >
      That's because setting a webcontrol's Visible property to "true" prevents
      that control from being rendered to the client browser.
      >
      I think I'm resigned to using a multi-line textbox to just let the
      user enter them in all at once, but I hate the idea because it leads
      the user down a path of potentially making many mistakes as he/she
      enters.  I just can't believe that their is no way to keep this data.
      What am I missing?
      >
      A hidden textbox is definitely the way to go here, so I'd suggest that your
      JavaScript is where the problem lies.
      >
      Please show your JavaScript and how you call it.
      >
      --
      Mark Rae
      ASP.NET MVPhttp://www.markrae.net
      Using runat="server" for the input box did the trick. I was unaware
      you could get an input box to run at server. Thank you very much! I
      do web development in an on again/ off again type manner so I seem to
      forget as much as I learn!

      For this comment though:
      If your JavaScript is supposed to select all of the elements in a ListBox
      but, in fact, clears the ListBox, then there is either a bug in your
      JavaScript, or it's not running at the correct place in the page cycle. This
      would need to run in the OnClientClick event of the <asp:Button /control
      which does the postback.
      Nothing in my code is clearing the list box. I have put breakpoints
      through all my code and the listbox is cleared before ONE line of code
      is executed. Something about .NET is clearing that listbox. So I
      couldn't even get my code to execute because by the time it tried, the
      list box was already cleared. This is perplexing to me. I'm not sure
      I understand the purpose of values going away like that.

      But it works now with the hidden text box option. So I'm happy.
      Thanks again!

      Comment

      • Vinay Khaitan

        #4
        Re: Listbox data going away

        May be you misunderstood the advice.
        I was told the issue is that the only thing retained in the list box
        is the item selected. So I tried to write a javascript method that
        You were told the right thing.
        would select all of them when the user clicks the "Run Report"
        button. It doesn't work, because I have to register the javascript
        code in my code behind and it clears that listbox before it executes
        even one line of code in the codebehind.
        First of all, there is no need to write javascript in code behind.
        You can write directly on aspx page with script tag.

        Once you populate listbox (i.e. <selecttag) using javascript and select
        all of them (make sure that listbox is multi-selectable), you can submit
        it.

        But remember, you can never access submitted code via listbox.
        You need to access them from postback data. That data can be
        got using Request.Forms["Listbox.Unique ID"] . This definitely
        would contain all of the options.
        Listbox is the best option, surely, unless you create your own custom
        control with ajax support.

        --
        Vinay Khaitan
        [Windows Forms Layout Control]

        ----------------------------------------------------------------



        Comment

        • Mark Rae [MVP]

          #5
          Re: Listbox data going away

          "Doogie" <dnlwhite@dtgne t.comwrote in message
          news:c7fa7f76-a39d-4f3a-a8b2-b733f4b0f600@v2 2g2000pro.googl egroups.com...
          Nothing in my code is clearing the list box. I have put breakpoints
          through all my code and the listbox is cleared before ONE line of code
          is executed. Something about .NET is clearing that listbox. So I
          couldn't even get my code to execute because by the time it tried, the
          list box was already cleared. This is perplexing to me. I'm not sure
          I understand the purpose of values going away like that.
          A ListBox will only "remember" pre-populated elements - client-side changes
          made to the list of element will disappear as soon as the postback happens.
          That's why you need to copy them into the hidden textbox *before* the
          postback - that is what the <asp:Button />'s OnClientClick method is for...


          --
          Mark Rae
          ASP.NET MVP


          Comment

          • Mark Rae [MVP]

            #6
            Re: Listbox data going away

            "Vinay Khaitan" <vkhaitan@gmail .comwrote in message
            news:OvSejBOQJH A.4144@TK2MSFTN GP06.phx.gbl...
            May be you misunderstood the advice.
            I believe it is you who have misunderstood the question...
            But remember, you can never access submitted code via listbox.
            You need to access them from postback data. That data can be
            got using Request.Forms["Listbox.Unique ID"] . This definitely
            would contain all of the options.
            No it wouldn't...

            The OP is not trying to query which of a listbox's *pre-populated* elements
            are *selected*...

            Client-side changes to the contents of a listbox's elements collection will
            not survive the postback - this is why the changes need to be copied into a
            hidden field first...


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • Vinay Khaitan

              #7
              Re: Listbox data going away

              may be you have overused ASP.NET.
              Server-side or client-side, all changes are posted back. it is HTTP
              features. try to understand POST protocol.
              The OP is not trying to query which of a listbox's *pre-populated*
              elements are *selected*...
              Browser doesn't remember, which of the elements are "pre-populated". HTML is
              stateless. Hence pre or post population doesn't matter.
              Come on, I have done it so many times.


              --
              Vinay Khaitan
              [Windows Forms Layout Control]

              ----------------------------------------------------------------





              Comment

              • Mark Rae [MVP]

                #8
                Re: Listbox data going away

                "Vinay Khaitan" <vkhaitan@gmail .comwrote in message
                news:eTAO6kOQJH A.4992@TK2MSFTN GP05.phx.gbl...
                may be you have overused ASP.NET.
                Server-side or client-side, all changes are posted back. it is HTTP
                features. try to understand POST protocol.
                1) Create an aspx page as follows:

                <head>
                <script type="text/javascript">
                function addElements()
                {
                var lstBox = document.getEle mentById('<%=My ListBox.ClientI D%>');
                lstBox.options[lstBox.length] = new Option('First') ;
                lstBox.options[lstBox.length] = new Option('Second' );
                lstBox.options[lstBox.length] = new Option('Third') ;
                }
                </script>
                </head>
                <body>
                <form id="form1" runat="server">
                <asp:ListBox ID="MyListBox" runat="server" />
                <input type="button" id="MyAddButton " value="Add elements"
                onclick="addEle ments();" />
                <asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
                OnClick="MySubm itButton_Click" />
                </form>
                </body>

                2) Add the appropriate server-side MySubmitButton_ Click method and set a
                breakpoint in it.

                3) Launch the page in debug mode - how many elements does the listbox have?

                4) Click the Add elements button - how many elements does the listbox have
                now?

                5) Hit the Submit button - when the code breaks in the MySubmitButton_ Click
                method, how many of the dynamically added listbox elements have survived the
                postback...?


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • Vinay Khaitan

                  #9
                  Re: Listbox data going away

                  See My comments inline:-
                  1) Create an aspx page as follows:
                  >
                  <head>
                  <script type="text/javascript">
                  function addElements()
                  {
                  var lstBox = document.getEle mentById('<%=My ListBox.ClientI D%>');
                  lstBox.options[lstBox.length] = new Option('First') ;
                  lstBox.options[lstBox.length] = new Option('Second' );
                  lstBox.options[lstBox.length] = new Option('Third') ;
                  }
                  </script>
                  </head>
                  <body>
                  <form id="form1" runat="server">
                  <asp:ListBox ID="MyListBox" runat="server" />
                  <input type="button" id="MyAddButton " value="Add elements"
                  onclick="addEle ments();" />
                  <asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
                  OnClick="MySubm itButton_Click" />
                  </form>
                  </body>
                  Created. I also set the property of Listbox as multiple selection in
                  designer.

                  2) Add the appropriate server-side MySubmitButton_ Click method and set a
                  breakpoint in it.
                  >
                  Done.
                  3) Launch the page in debug mode - how many elements does the listbox
                  have?
                  None.
                  >
                  4) Click the Add elements button - how many elements does the listbox have
                  now?
                  3. "First", "Second", "Third"
                  5) Hit the Submit button - when the code breaks in the
                  MySubmitButton_ Click method, how many of the dynamically added listbox
                  elements have survived the postback...?
                  I selected all three. at breakpoint checked property
                  Request.Form[1]. Its value is :-

                  "First","Second ","Third" .

                  Actually this is the first time I am seeing how the multiple selectable
                  listbox/dropdown
                  are represented in HTTP Postback, although I knew that something like this
                  happens.

                  I would again say. Don't be spoilt by ASP.NET. Also try to know HTTP
                  protocol.
                  Why don't you yourself try it once ??


                  >
                  >
                  --
                  Mark Rae
                  ASP.NET MVP
                  http://www.markrae.net
                  --
                  Vinay Khaitan
                  [Windows Forms Layout Control]

                  ----------------------------------------------------------------


                  Comment

                  • Vinay Khaitan

                    #10
                    Re: Listbox data going away

                    After I tested with your code, I can now confirmingly say that THIS WORKS.
                    and Doogie can use it. the list comes in comma separated string.

                    --
                    Vinay Khaitan
                    [Windows Forms Layout Control]

                    ----------------------------------------------------------------


                    "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                    news:OoIt25OQJH A.588@TK2MSFTNG P06.phx.gbl...
                    "Vinay Khaitan" <vkhaitan@gmail .comwrote in message
                    news:eTAO6kOQJH A.4992@TK2MSFTN GP05.phx.gbl...
                    >
                    >may be you have overused ASP.NET.
                    >Server-side or client-side, all changes are posted back. it is HTTP
                    >features. try to understand POST protocol.
                    >
                    1) Create an aspx page as follows:
                    >
                    <head>
                    <script type="text/javascript">
                    function addElements()
                    {
                    var lstBox = document.getEle mentById('<%=My ListBox.ClientI D%>');
                    lstBox.options[lstBox.length] = new Option('First') ;
                    lstBox.options[lstBox.length] = new Option('Second' );
                    lstBox.options[lstBox.length] = new Option('Third') ;
                    }
                    </script>
                    </head>
                    <body>
                    <form id="form1" runat="server">
                    <asp:ListBox ID="MyListBox" runat="server" />
                    <input type="button" id="MyAddButton " value="Add elements"
                    onclick="addEle ments();" />
                    <asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
                    OnClick="MySubm itButton_Click" />
                    </form>
                    </body>
                    >
                    2) Add the appropriate server-side MySubmitButton_ Click method and set a
                    breakpoint in it.
                    >
                    3) Launch the page in debug mode - how many elements does the listbox
                    have?
                    >
                    4) Click the Add elements button - how many elements does the listbox have
                    now?
                    >
                    5) Hit the Submit button - when the code breaks in the
                    MySubmitButton_ Click method, how many of the dynamically added listbox
                    elements have survived the postback...?
                    >
                    >
                    --
                    Mark Rae
                    ASP.NET MVP
                    http://www.markrae.net

                    Comment

                    • Mark Rae [MVP]

                      #11
                      Re: Listbox data going away

                      "Vinay Khaitan" <vkhaitan@gmail .comwrote in message
                      news:ugW%23TUPQ JHA.1876@TK2MSF TNGP06.phx.gbl. ..
                      I selected all three
                      Why did you do that? I didn't ask you to do that! Read the OP again - the
                      point of this is *not* to count the number of *selected* elements...

                      Try it again, but this time *don't* select any of the listbox elements...

                      Now what is the value of Request.Form[1]...?


                      --
                      Mark Rae
                      ASP.NET MVP


                      Comment

                      • Mark Rae [MVP]

                        #12
                        Re: Listbox data going away

                        "Vinay Khaitan" <vkhaitan@gmail .comwrote in message
                        news:Orq9bVPQJH A.4232@TK2MSFTN GP03.phx.gbl...
                        After I tested with your code, I can now confirmingly say that THIS WORKS.
                        and Doogie can use it. the list comes in comma separated string.
                        Don't select any of the dynamically added elements...


                        --
                        Mark Rae
                        ASP.NET MVP


                        Comment

                        • Vinay Khaitan

                          #13
                          Re: Listbox data going away

                          oh, come on,
                          "So I tried to write a javascript method that
                          would select all of them when the user clicks the "Run Report"
                          button. "

                          That is a quote from Doogie. He tried to select it through javascript, but
                          still didn't get it to work.
                          You just need a trick to submit the data and I gave that trick.
                          WHY NOT SELECT DYNAMIC ELELMENT ?

                          --
                          Vinay Khaitan
                          [Windows Forms Layout Control]

                          ----------------------------------------------------------------


                          "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                          news:%23reozbPQ JHA.1148@TK2MSF TNGP05.phx.gbl. ..
                          "Vinay Khaitan" <vkhaitan@gmail .comwrote in message
                          news:Orq9bVPQJH A.4232@TK2MSFTN GP03.phx.gbl...
                          >
                          >After I tested with your code, I can now confirmingly say that THIS
                          >WORKS. and Doogie can use it. the list comes in comma separated string.
                          >
                          Don't select any of the dynamically added elements...
                          >
                          >
                          --
                          Mark Rae
                          ASP.NET MVP
                          http://www.markrae.net

                          Comment

                          • Mark Rae [MVP]

                            #14
                            Re: Listbox data going away

                            "Vinay Khaitan" <vkhaitan@gmail .comwrote in message
                            news:efBcmgPQJH A.444@TK2MSFTNG P05.phx.gbl...
                            >>After I tested with your code, I can now confirmingly say that THIS
                            >>WORKS. and Doogie can use it. the list comes in comma separated string.
                            >>
                            >Don't select any of the dynamically added elements...
                            >
                            WHY NOT SELECT DYNAMIC [ELELMENT] ELEMENT ?
                            BECAUSE THAT'S NOT WHAT THE OP ACTUALLY WANTED TO DO!!!


                            --
                            Mark Rae
                            ASP.NET MVP


                            Comment

                            • Vinay Khaitan

                              #15
                              Re: Listbox data going away

                              BECAUSE THAT'S NOT WHAT THE OP ACTUALLY WANTED TO DO!!!
                              Oh, my god. Doogie tried it but could not get it right. Okay, let the doogie
                              reply to it. If it works for him, why to bother.

                              --
                              Vinay Khaitan
                              [Windows Forms Layout Control]

                              ----------------------------------------------------------------



                              Comment

                              Working...