使用 AddRange 方法将多个 ListItem 对象添加到集合

 

ListItemCollection myListItemCollection = new ListItemCollection();

// Creates an array of ListItems that are used to populate the collection.    
System.Web.UI.WebControls.ListItem[] newListItemArray = new ListItem[tempDataTable.Count];

// Populates the array of ListItems with generic data.
for (int i=0; i < tempDataTable.Count; i++) {
    newListItemArray[i] = new ListItem();
    newListItemArray[i].Text = "Item " + i.ToString();
    newListItemArray[i].Value = i.ToString();
}

// Adds an entire array of ListItem objects to the collection.
myListItemCollection.AddRange(newListItemArray);
posted @ 2008-06-26 06:57  jianfangkk  阅读(695)  评论(0编辑  收藏  举报