I have a list box control which I am filling from the page's code behind.
It works with values from 1 to 100, but what if the range I need gets really large? (For example, 1 to 200000000?)
Currently I am using this:
<asp:ListBox ID="ListBox1" runat="server" EnableTheming="True"
Height="20px" Width="54px">
</asp:ListBox>
And in my code behind:
for (int i = 0开发者_JAVA技巧; i < 100; i++)
{
ListBox1.Items.Add(i.ToString());
}
What is the correct way to approach this problem?
You could use the NumericUpDown tool from the AJAX toolkit. It lets you enter numbers as well as scroll up and down, without having a varying scrollbar.
Have you considered what this listbox is going to look like if it has 200 million choices?
What if someone wants to choose 140,235?
Perhaps you could think of a different way to have the user select the number?
Could they enter it in a textbox?
I hate to say it, but don't. Think about your users -- if they need to scroll through (per your hopefully hypothetical example) 200,000,000 items, they won't be able to use the list box. Additionally, you'll overload the heck out of the browser with that kind of data.
Find another means of doing this. You could allow free form text entry (and display an error if the user has entered non-numeric data -- the RangeValidator
will help there), you could use (as TenaciousImpy stated) the NumericUpDown
AJAX tool. There are other options.
Generally speaking, list boxes and drop down list boxes should not normally have more than a hundred or so items.
Or, as Raymond Chen has said in the past, if you have to ask about limits, you're probably doing something wrong.
If you are trying to put that many records into a dropdown list, you are doing something wrong.
Normally, with a large amount of records, one shows a subset of it (for example, only 100 out of hunderd thousand), and then page through to the next 100 etc...
If you want to restrict user input to positive integers from 1..n, how about using a TextBox control, along with a RangeValidator to validate the range?
If you want to allow multiple selection of values, give the user multiple TextBoxes (could dynamically generate), or consider simply allowing the user to enter a comma separated list of numbers and parse it in your code behind. I find most users prefer keyboard data entry as opposed to "mouse click" data entry - it is a lot faster.
精彩评论