Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. !!!Question about Programmatically adding items to list control

!!!Question about Programmatically adding items to list control

Scheduled Pinned Locked Moved ASP.NET
performancequestionhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vicky457
    wrote on last edited by
    #1

    I need to fill a dropdownlist with 1 to 60. So I did it by doing the following code: for(int i=0; i <=60; i++) DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString())) However, I was thinking this probally is not good idea to use "new ListItem" each time. So I replace the above with the following: ListItem li = new ListItem(); for(int i=0; i <=60; i++) { li.Text = i.ToString(); li.Value = i.ToString(); DropdownList1.Items.Add(li) } But the result turned to be all the items in the list are 60. Is there anything in the second code wrong? I have seen people using the first code all the time, but bot the second code. Does anybody know why? Could anybody give me some ideas why the second code doesn't work? What's the life cycle of the new item when using DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString())) The last IMPORTANT question is: Does using "new listitem" will cause performance issue, such as more memory allocation, etc? I would really appreciate any ideas! Thank you in advance!

    E C 2 Replies Last reply
    0
    • V vicky457

      I need to fill a dropdownlist with 1 to 60. So I did it by doing the following code: for(int i=0; i <=60; i++) DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString())) However, I was thinking this probally is not good idea to use "new ListItem" each time. So I replace the above with the following: ListItem li = new ListItem(); for(int i=0; i <=60; i++) { li.Text = i.ToString(); li.Value = i.ToString(); DropdownList1.Items.Add(li) } But the result turned to be all the items in the list are 60. Is there anything in the second code wrong? I have seen people using the first code all the time, but bot the second code. Does anybody know why? Could anybody give me some ideas why the second code doesn't work? What's the life cycle of the new item when using DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString())) The last IMPORTANT question is: Does using "new listitem" will cause performance issue, such as more memory allocation, etc? I would really appreciate any ideas! Thank you in advance!

      E Offline
      E Offline
      error1408
      wrote on last edited by
      #2

      In the second code you add the SAME list item 60 times. Now if you change the items text and value you change ALL items. Everytime you add that listitem to the dropdownlist you add a REFERENCE and NOT a COPY to the list. You could, for example, add that listitem to another dropdownlist without any problems. And changing it would change the value in both dropdownlists. I hope i could help you. If not just say it :D

      V 1 Reply Last reply
      0
      • E error1408

        In the second code you add the SAME list item 60 times. Now if you change the items text and value you change ALL items. Everytime you add that listitem to the dropdownlist you add a REFERENCE and NOT a COPY to the list. You could, for example, add that listitem to another dropdownlist without any problems. And changing it would change the value in both dropdownlists. I hope i could help you. If not just say it :D

        V Offline
        V Offline
        vicky457
        wrote on last edited by
        #3

        Thanks for your respones. It looks like the first approach is the only way to programmatically adding item to asp:dropdownlist.

        1 Reply Last reply
        0
        • V vicky457

          I need to fill a dropdownlist with 1 to 60. So I did it by doing the following code: for(int i=0; i <=60; i++) DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString())) However, I was thinking this probally is not good idea to use "new ListItem" each time. So I replace the above with the following: ListItem li = new ListItem(); for(int i=0; i <=60; i++) { li.Text = i.ToString(); li.Value = i.ToString(); DropdownList1.Items.Add(li) } But the result turned to be all the items in the list are 60. Is there anything in the second code wrong? I have seen people using the first code all the time, but bot the second code. Does anybody know why? Could anybody give me some ideas why the second code doesn't work? What's the life cycle of the new item when using DropdownList1.Items.Add(new ListItem(i.ToString(), i.ToString())) The last IMPORTANT question is: Does using "new listitem" will cause performance issue, such as more memory allocation, etc? I would really appreciate any ideas! Thank you in advance!

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          vicky457 wrote:

          The last IMPORTANT question is: Does using "new listitem" will cause performance issue, such as more memory allocation, etc?

          Yes, it means you end up with 60 items, which is what you want. Otherwise, you have one item, which you keep changing the value of.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups