值不在预期范围内 [英] Value does not fall within the expected range

查看:69
本文介绍了值不在预期范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码更新列表框,从而从Web服务接收列表:

I am using the following code to update a listbox, this recieving a list from a Web service:

client.userKeywordsCompleted += new EventHandler<userKeywordsCompletedEventArgs>(client_userKeywordsCompleted);
client.userKeywordsAsync();

使用:

void client_userKeywordsCompleted(object sender, userKeywordsCompletedEventArgs e)
{

    string result = System.Convert.ToString(e.Result);

    for (int i = 0; i < e.Result.Count; i++)
    {

        ListBoxItem lbitem = new ListBoxItem();

        lbitem.Name = "lb_" + i;
        lbitem.Content = e.Result[i];

        lbitem.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(ListBoxItem_DoubleClickEvent), true);

        listBox1.Items.Add(lbitem);

    }

这很好用,因为我在加载子窗口"时使用它,所以ListBox从数据库中获取列表,但是,当用户选择ListBox中的一项时,他们可以选择编辑所选项目.因此,一旦编辑到位,就会有一个编辑按钮,该按钮将更新数据库表中的列.因此,然后单击按钮,我再次调用上述代码以使用新凭据更新ListBox.但是,这会带来错误-

This works fine, as I use it when the Child window loads, so the ListBox gets the list from the database, however, when a user selects one of the items in the ListBox, they have the option to edit the selected item. So once the edit is in place, there is an edit button, which updates the column in the table in the database. So then on the button click, I am again calling the aforementioned code to update the ListBox with the new credentials. However, this brings back the error -

"Value does not fall within the expected range."

为什么我不能在单击按钮时调用Web方法,因为它所做的只是刷新ListBox ???

Why can I not call the Web method on the button click, as all it is doing is refreshing the ListBox???

推荐答案

这可能是由于您试图向页面添加具有相同名称的ListBoxItem造成的.

This might be due to the fact that you are trying to add a ListBoxItem with a same name to the page.

如果要使用新检索的值刷新列表框的内容,则必须首先手动删除列表框的内容,否则循环将尝试再次创建lb_1并将其添加到同一列表中.

If you want to refresh the content of the listbox with the newly retrieved values you will have to first manually remove the content of the listbox other wise your loop will try to create lb_1 again and add it to the same list.

在这里查看发生的类似问题 Silverlight:值不在预期范围内

Look at here for a similar problem that occured Silverlight: Value does not fall within the expected range exception

干杯

这篇关于值不在预期范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆