!!!Question about Programmatically adding items to list control
-
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!
-
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!
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
-
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
-
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!
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 )