如何:在集合中设置 Web 服务器控件属性

更新:2007 年 11 月

某些 Web 服务器控件的属性不是简单的值或对象,而是集合。例如,ListBox Web 服务器控件的单个值实现为 ListItem 对象的集合。

设置基于集合的控件属性

  • 实例化要使用的项,然后将其添加到控件的集合。

    下面的代码示例演示如何通过将 ListItem 对象添加到 ListBox 控件的 Items 集合中,来将该对象添加到此控件中。在第一个示例中,项在添加之前是显式创建的。在第二个示例中,项的创建和添加是同时进行的。

    Dim li As ListItem = New ListItem("Item 1")
    ListBox1.Items.Add(li)
    
    ' Create and add the items at the same time
    ListBox1.Items.Add(New ListItem("Apples"))
    ListBox1.Items.Add(New ListItem("Oranges"))
    ListBox1.Items.Add(New ListItem("Lemons"))
    
    // Create the items and then add them to the list.
    ListItem li = new ListItem("Item 1");
    ListBox1.Items.Add(li);
    
    // Create and add the items at the same time.
    ListBox1.Items.Add(new ListItem("Apples"));
    ListBox1.Items.Add(new ListItem("Oranges"));
    ListBox1.Items.Add(new ListItem("Lemons"));
    

请参见

任务

如何:设置 ASP.NET 服务器控件属性

其他资源

以编程方式设置 ASP.NET 服务器控件属性