请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
System.Web.UI
Control 类
Control 方法
 FindControl 方法 (String)

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
Control.FindControl 方法 (String)

在当前的命名容器中搜索带指定 id 参数的服务器控件。

命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public Overridable Function FindControl ( _
    id As String _
) As Control
Visual Basic(用法)
Dim instance As Control
Dim id As String
Dim returnValue As Control

returnValue = instance.FindControl(id)
C#
public virtual Control FindControl (
    string id
)
C++
public:
virtual Control^ FindControl (
    String^ id
)
J#
public Control FindControl (
    String id
)
JScript
public function FindControl (
    id : String
) : Control

参数

id

要查找的控件的标识符。

返回值

指定的控件,或为 空引用(在 Visual Basic 中为 Nothing)(如果指定的控件不存在的话)。

下面的示例定义了一个 Button1_Click 事件处理程序。在被调用时,此处理程序使用 FindControl 方法来在包含页上查找 ID 属性为 TextBox2 的控件。如果找到了该控件,则使用 Parent 属性确定其父控件,并将父控件的 ID 写入页中。如果没有找到 TextBox2,则将“Control Not Found”写入页中。

Visual Basic
Private Sub Button1_Click(sender As Object, MyEventArgs As EventArgs)
      ' Find control on page.
      Dim myControl1 As Control = FindControl("TextBox2")
      If (Not myControl1 Is Nothing)
         ' Get control's parent.
         Dim myControl2 As Control = myControl1.Parent
         Response.Write("Parent of the text box is : " & myControl2.ID)
      Else
         Response.Write("Control not found.....")
      End If
End Sub

C#
private void Button1_Click(object sender, EventArgs MyEventArgs)
{
      // Find control on page.
      Control myControl1 = FindControl("TextBox2");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
         Response.Write("Parent of the text box is : " + myControl2.ID);
      }
      else
      {
         Response.Write("Control not found");
      }
}
J#
private void Button1_Click(Object sender, EventArgs myEventArgs)
{
    // Find control on page.
    Control myControl1 = FindControl("TextBox2");
    if (myControl1  != null) {
        // Get control's parent.
        Control myControl2 = myControl1.get_Parent();
        this.get_Response().Write("Parent of the text box is : " 
            + myControl2.get_ID());
    }
    else {
        this.get_Response().Write("Control not found");
    }
}//Button1_Click

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker