ControlIDConverter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供型別轉換子 (Type Converter),以便擷取目前容器中控制項 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 轉譯控件識別碼的 類別中使用 。
DebugInfoControl
是一個簡單的控件,可列印目前Web Form所包含之控件的一些資訊。 屬性 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 Form 中使用 ,以及 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 類別,並提供控件標識符清單,以便顯示在設計時間環境中的屬性方格控件中。 類別ControlIDConverter也做為和 ValidatedControlConverter 類別的基類AssociatedControlConverter,這些類別是分別支持驗證屬性屬性之 Web 控件和控件的類型轉換子。
如需類型轉換器的詳細資訊,請參閱 如何:實作類型轉換器 或 一般化類型轉換。
建構函式
ControlIDConverter() |
初始化 ControlIDConverter 類別的新執行個體。 |