foreach (ListItem Item in ClassID.Items)
            {
 if (Item.Value != "Null")
                {
                    Item.Selected = true;
                }
        }
//在多个选项的值为“Null”,如何让其不会出错,并选中值为“Null”的第一个选项
在多个选项的值为“Null”,如何让其不会出错,并选中值为“Null”的第一个选项

解决方案 »

  1.   

    你要选中值为NULL的第一个选项,为什么要判断Item.Value != "Null" 
      

  2.   


    啊呀,提错了,应该为:在多个选项的值为非“Null”时,如何让其不会出错,并选中值为非“Null”的第一个选项
      

  3.   


    foreach (ListItem Item in ClassID.Items)
    {
        if (Item.Value != "Null")
        {
            Item.Selected = true;
            break;
        }
    }
      

  4.   

    如果不需要全部遍历,只是绑定break是可以,如果楼主需要全部遍历一遍可以定义一个变量用来记录第一次出现值不为NULL的Index,遍历完再设置选中项的Index,如:            int index = -1;
                for (int i = 0; i < ClassID.Items.Count; i++)
                {
                    if (ClassID.Items[i].Value != null)
                    {
                        if (index == -1)
                        {
                            index = i;
                        }
                    }
                }            ClassID.Items[index].Selected = true;//这里要判断下index是不是等于-1
      

  5.   

    再要跳出循环的地方加个break