0

我在哪里可以在 asp.net 生命周期方法中创建此对象而不会收到超出范围的异常。现在,我真正可以获得包含值的资源集合的唯一地方是用于 webschedule 信息的 onreasourcefetched 方法。但是我需要在创建 webscheduleinfo 并且它的视图中填充用户之前执行此操作。

protected void Page_Init(object sender, EventArgs e)
        {
            ResourcesCollection resources = WebScheduleInfo1.VisibleResources;

            int count = resources.Count;
            Resource obje = (Resource)resources.GetItem(1);
            string name = obje.Name;
            resources.Clear();
            resources.Add(obje);
            this.WebScheduleInfo1.ActiveResourceName = name;
        }
4

1 回答 1

2

You are getting a count of resources but you are not checking to make sure that count is greater than 0.

(Resource)resources.GetItem(1) will fail unless the resources collection has at least 2 items in it.

The collection is 0 based, so if you want the first item do something like this:

protected void Page_Init(object sender, EventArgs e)
{
    ResourcesCollection resources = WebScheduleInfo1.VisibleResources;

    int count = resources.Count;

    if( count > 0 )
    {
        Resource obje = (Resource)resources.GetItem(0);
        string name = obje.Name;
        resources.Clear();
        resources.Add(obje);
        this.WebScheduleInfo1.ActiveResourceName = name;
    }
}
于 2013-02-21T16:58:22.473 回答