EditorPartCollection.IndexOf(EditorPart) 方法

定义

返回集合中特定成员的位置。

public:
 int IndexOf(System::Web::UI::WebControls::WebParts::EditorPart ^ editorPart);
public int IndexOf (System.Web.UI.WebControls.WebParts.EditorPart editorPart);
member this.IndexOf : System.Web.UI.WebControls.WebParts.EditorPart -> int
Public Function IndexOf (editorPart As EditorPart) As Integer

参数

editorPart
EditorPart

EditorPart,它是集合的成员。

返回

一个整数,对应于集合中的 EditorPart 控件的索引。

示例

下面的代码示例演示如何使用 IndexOf 方法在 对象中EditorPartCollection查找EditorPart控件。 有关运行示例所需的完整代码,请参阅类概述的 EditorPartCollection “示例”部分。

事件中的 Button1_Click 代码创建一个 EditorPartCollection 对象,然后使用 IndexOf 方法在集合中查找 PropertyGridEditorPart1 控件,并设置其 ChromeType 属性。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
  {
    ArrayList list = new ArrayList(2);
    list.Add(AppearanceEditorPart1);
    list.Add(PropertyGridEditorPart1);
    // Pass an ICollection object to the constructor.
    EditorPartCollection myParts = new EditorPartCollection(list);
    foreach (EditorPart editor in myParts)
    {
      editor.BackColor = System.Drawing.Color.LightBlue;
      editor.Description = "My " + editor.DisplayTitle + " editor.";
    }

    // Use the IndexOf property to locate an EditorPart control.
    int propertyGridPart = myParts.IndexOf(PropertyGridEditorPart1);
    myParts[propertyGridPart].ChromeType = PartChromeType.TitleOnly;

    // Use the Contains method to see if an EditorPart exists.
    if(!myParts.Contains(LayoutEditorPart1))
      LayoutEditorPart1.BackColor = System.Drawing.Color.LightYellow;
    
    // Use the CopyTo method to create an array of EditorParts.
    EditorPart[] partArray = new EditorPart[3];
    partArray[0] = LayoutEditorPart1;
    myParts.CopyTo(partArray,1);
    Label1.Text = "<h3>EditorParts in Custom Array</h3>";
    foreach (EditorPart ePart in partArray)
    {
      Label1.Text += ePart.Title + "<br />";
    }

  }

</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As EventArgs)
    
    Dim list As New ArrayList(2)
    list.Add(AppearanceEditorPart1)
    list.Add(PropertyGridEditorPart1)
    ' Pass an ICollection object to the constructor.
    Dim myParts As New EditorPartCollection(list)
    Dim editor As EditorPart
    For Each editor In myParts
      editor.BackColor = System.Drawing.Color.LightBlue
      editor.Description = "My " + editor.DisplayTitle + " editor."
    Next editor
    
    ' Use the IndexOf property to locate an EditorPart control.
    Dim propertyGridPart As Integer = _
      myParts.IndexOf(PropertyGridEditorPart1)
    myParts(propertyGridPart).ChromeType = PartChromeType.TitleOnly
    
    ' Use the Contains method to see if an EditorPart exists.
    If Not myParts.Contains(LayoutEditorPart1) Then
      LayoutEditorPart1.BackColor = System.Drawing.Color.LightYellow
    End If
    
    ' Use the CopyTo method to create an array of EditorParts.
    Dim partArray(2) As EditorPart
    partArray(0) = LayoutEditorPart1
    myParts.CopyTo(partArray, 1)
    Label1.Text = "<h3>EditorParts in Custom Array</h3>"
    Dim ePart As EditorPart
    For Each ePart In partArray
      Label1.Text += ePart.Title + "<br />"
    Next ePart

  End Sub

</script>

在浏览器中加载页面时,可以通过在“显示模式”下拉列表控件中选择“编辑”,将页面切换到编辑模式。 可以单击“谓词”菜单 (控件标题栏中 TextDisplayWebPart 的向下箭头) ,然后单击“ 编辑 ”以编辑控件。 当编辑用户界面 (UI) 可见时,可以看到所有 EditorPart 控件。 如果单击“ 创建编辑器”“PartCollection ”按钮,将注意到 PropertyGridEditorPart1 靠近页面底部的控件具有标题,但没有边框。

注解

IndexOf如果 Web 部件页上有多个EditorPart控件,并且需要在集合中查找特定控件,则 此方法非常有用。

适用于

另请参阅