ControlIDConverter 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供检索当前容器中控件 ID 的列表的类型转换器。
public ref class ControlIDConverter : System::ComponentModel::StringConverter
public class ControlIDConverter : System.ComponentModel.StringConverter
type ControlIDConverter = class
inherit StringConverter
Public Class ControlIDConverter
Inherits StringConverter
- 继承
- 派生
示例
下面的代码示例演示如何 ControlIDConverter 在需要 TypeConverter 呈现控件 ID 的类中使用 。
DebugInfoControl
是一个简单的控件,它打印出当前 Web 窗体包含的控件上的一些信息。 其 ControlID
属性使用 TypeConverterAttribute 修饰,该属性将 指定 ControlIDConverter 为 TypeConverter 要用于该属性的 。 重写 DebugInfoControl
方法以 Render
打印控件中 Label 有关目标控件的信息。
namespace Samples.AspNet.CS {
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
[DefaultProperty("ControlID")]
public class DebugInfoControl : Control {
public DebugInfoControl() {
}
public DebugInfoControl(string controlID) {
ControlID = controlID;
}
[DefaultValue(""),
TypeConverter(typeof(ControlIDConverter))]
public string ControlID {
get {
object o = ViewState["ControlID"];
if (o == null)
return String.Empty;
return (string)o;
}
set {
if (ControlID != value) {
ViewState["ControlID"] = value;
}
}
}
protected override void Render(HtmlTextWriter writer) {
Label info = new Label();
if (this.ControlID.Length == 0) {
writer.Write("<Font Color='Red'>No ControlID set.</Font>");
}
Control ctrl = this.FindControl(ControlID);
if (ctrl == null) {
writer.Write("<Font Color='Red'>Could not find control " + ControlID + " in Naming Container.</Font>");
}
else {
writer.Write("<Font Color='Green'>Control " + ControlID + " found.<BR>");
writer.Write("Its Naming Container is: " + ctrl.NamingContainer.ID + "<BR>");
if (ctrl.EnableViewState)
writer.Write("It uses view state to persist its state.<BR>");
if (ctrl.EnableTheming)
writer.Write("It can be assigned a Theme ID.<BR>");
if (ctrl.Visible)
writer.Write("It is visible on the page.<BR>");
else
writer.Write("It is not visible on the page.<BR>");
writer.Write("</Font>");
}
}
}
}
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB
<DefaultProperty("ControlID")> _
Public Class DebugInfoControl
Inherits Control
Public Sub New()
End Sub
Public Sub New(controlID As String)
ControlID = controlID
End Sub
<DefaultValue(""), TypeConverter(GetType(ControlIDConverter))> _
Public Property ControlID() As String
Get
Dim o As Object = ViewState("ControlID")
If o Is Nothing Then
Return String.Empty
End If
Return CStr(o)
End Get
Set
If ControlID <> value Then
ViewState("ControlID") = value
End If
End Set
End Property
Protected Overrides Sub Render(writer As HtmlTextWriter)
Dim info As New Label()
If Me.ControlID.Length = 0 Then
writer.Write("<Font Color='Red'>No ControlID set.</Font>")
End If
Dim ctrl As Control = Me.FindControl(ControlID)
If ctrl Is Nothing Then
writer.Write(("<Font Color='Red'>Could not find control " + ControlID + " in Naming Container.</Font>"))
Else
writer.Write(("<Font Color='Green'>Control " + ControlID + " found.<BR>"))
writer.Write(("Its Naming Container is: " + ctrl.NamingContainer.ID + "<BR>"))
If ctrl.EnableViewState Then
writer.Write("It uses view state to persist its state.<BR>")
End If
If ctrl.EnableTheming Then
writer.Write("It can be assigned a Theme ID.<BR>")
End If
If ctrl.Visible Then
writer.Write("It is visible on the page.<BR>")
Else
writer.Write("It is not visible on the page.<BR>")
End If
writer.Write("</Font>")
End If
End Sub
End Class
End Namespace
下面的代码示例演示如何 DebugInfoControl
在 Web 窗体中与 控件一 AccessDataSource 起使用以显示有关控件 AccessDataSource 的信息。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" assembly="Samples.AspNet.CS" %>
<%@Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:accessdatasource
id="AccessDataSource1"
runat="server"
datasourcemode="DataReader"
datafile="Northwind.mdb"
selectcommand="SELECT OrderID FROM Orders WHERE EmployeeID=2">
</asp:accessdatasource>
<br />
<aspSample:debuginfocontrol
id="DebugInfoControl1"
runat="server"
controlid="AccessDataSource1" />
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:accessdatasource
id="AccessDataSource1"
runat="server"
datasourcemode="DataReader"
datafile="Northwind.mdb"
selectcommand="SELECT OrderID FROM Orders WHERE EmployeeID=2">
</asp:accessdatasource>
<br />
<aspSample:debuginfocontrol
id="DebugInfoControl1"
runat="server"
controlid="AccessDataSource1" />
</form>
</body>
</html>
注解
ControlIDConverter类派生自 StringConverter 类,并提供控件 ID 列表,以便在设计时环境中的属性网格控件中显示。 类ControlIDConverter还充当 和 ValidatedControlConverter 类的AssociatedControlConverter基类,它们分别是支持验证属性属性的 Web 控件和控件的类型转换器。
有关类型转换器的详细信息,请参阅 如何:实现类型转换器 或 通用类型转换。
构造函数
ControlIDConverter() |
初始化 ControlIDConverter 类的新实例。 |