แก้ไข

แชร์ผ่าน


ControlIDConverter Class

Definition

Provides a type converter that retrieves a list of control IDs in the current container.

public ref class ControlIDConverter : System::ComponentModel::StringConverter
public class ControlIDConverter : System.ComponentModel.StringConverter
type ControlIDConverter = class
    inherit StringConverter
Public Class ControlIDConverter
Inherits StringConverter
Inheritance
ControlIDConverter
Derived

Examples

The following code example demonstrates how to use the ControlIDConverter in a class that requires a TypeConverter to render the ID of a control. The DebugInfoControl is a simple control that prints out some information on a control contained by the current Web Form. Its ControlID property is decorated with a TypeConverterAttribute that designates the ControlIDConverter as the TypeConverter to use for that property. The DebugInfoControl overrides the Render method to print out information about the target control in a Label control.

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

The following code example demonstrates how the DebugInfoControl can be used in a Web Form along with an AccessDataSource control to display information about the AccessDataSource control.

<%@ 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>

Remarks

The ControlIDConverter class derives from the StringConverter class and provides a list of control IDs for display in a property grid control in design-time environments. The ControlIDConverter class also serves as the base class for the AssociatedControlConverter and ValidatedControlConverter classes, which are type converters for Web controls and controls that support validation property attributes, respectively.

For more information about type converters, see How to: Implement a Type Converter or Generalized Type Conversion.

Constructors

ControlIDConverter()

Initializes a new instance of the ControlIDConverter class.

Methods

CanConvertFrom(ITypeDescriptorContext, Type)

Gets a value indicating whether this converter can convert an object in the given source type to a string using the specified context.

(Inherited from StringConverter)
CanConvertFrom(Type)

Returns whether this converter can convert an object of the given type to the type of this converter.

(Inherited from TypeConverter)
CanConvertTo(ITypeDescriptorContext, Type)

Returns whether this converter can convert the object to the specified type, using the specified context.

(Inherited from TypeConverter)
CanConvertTo(Type)

Returns whether this converter can convert the object to the specified type.

(Inherited from TypeConverter)
ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

Converts the specified value object to a String object.

(Inherited from StringConverter)
ConvertFrom(Object)

Converts the given value to the type of this converter.

(Inherited from TypeConverter)
ConvertFromInvariantString(ITypeDescriptorContext, String)

Converts the given string to the type of this converter, using the invariant culture and the specified context.

(Inherited from TypeConverter)
ConvertFromInvariantString(String)

Converts the given string to the type of this converter, using the invariant culture.

(Inherited from TypeConverter)
ConvertFromString(ITypeDescriptorContext, CultureInfo, String)

Converts the given text to an object, using the specified context and culture information.

(Inherited from TypeConverter)
ConvertFromString(ITypeDescriptorContext, String)

Converts the given text to an object, using the specified context.

(Inherited from TypeConverter)
ConvertFromString(String)

Converts the specified text to an object.

(Inherited from TypeConverter)
ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

Converts the given value object to the specified type, using the specified context and culture information.

(Inherited from TypeConverter)
ConvertTo(Object, Type)

Converts the given value object to the specified type, using the arguments.

(Inherited from TypeConverter)
ConvertToInvariantString(ITypeDescriptorContext, Object)

Converts the specified value to a culture-invariant string representation, using the specified context.

(Inherited from TypeConverter)
ConvertToInvariantString(Object)

Converts the specified value to a culture-invariant string representation.

(Inherited from TypeConverter)
ConvertToString(ITypeDescriptorContext, CultureInfo, Object)

Converts the given value to a string representation, using the specified context and culture information.

(Inherited from TypeConverter)
ConvertToString(ITypeDescriptorContext, Object)

Converts the given value to a string representation, using the given context.

(Inherited from TypeConverter)
ConvertToString(Object)

Converts the specified value to a string representation.

(Inherited from TypeConverter)
CreateInstance(IDictionary)

Re-creates an Object given a set of property values for the object.

(Inherited from TypeConverter)
CreateInstance(ITypeDescriptorContext, IDictionary)

Creates an instance of the type that this TypeConverter is associated with, using the specified context, given a set of property values for the object.

(Inherited from TypeConverter)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FilterControl(Control)

Returns a value indicating whether the control ID of the specified control is added to the TypeConverter.StandardValuesCollection that is returned by the GetStandardValues(ITypeDescriptorContext) method.

GetConvertFromException(Object)

Returns an exception to throw when a conversion cannot be performed.

(Inherited from TypeConverter)
GetConvertToException(Object, Type)

Returns an exception to throw when a conversion cannot be performed.

(Inherited from TypeConverter)
GetCreateInstanceSupported()

Returns whether changing a value on this object requires a call to the CreateInstance(IDictionary) method to create a new value.

(Inherited from TypeConverter)
GetCreateInstanceSupported(ITypeDescriptorContext)

Returns whether changing a value on this object requires a call to CreateInstance(IDictionary) to create a new value, using the specified context.

(Inherited from TypeConverter)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetProperties(ITypeDescriptorContext, Object, Attribute[])

Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.

(Inherited from TypeConverter)
GetProperties(ITypeDescriptorContext, Object)

Returns a collection of properties for the type of array specified by the value parameter, using the specified context.

(Inherited from TypeConverter)
GetProperties(Object)

Returns a collection of properties for the type of array specified by the value parameter.

(Inherited from TypeConverter)
GetPropertiesSupported()

Returns whether this object supports properties.

(Inherited from TypeConverter)
GetPropertiesSupported(ITypeDescriptorContext)

Returns whether this object supports properties, using the specified context.

(Inherited from TypeConverter)
GetStandardValues()

Returns a collection of standard values from the default context for the data type this type converter is designed for.

(Inherited from TypeConverter)
GetStandardValues(ITypeDescriptorContext)

Returns a collection of control IDs from the container within the IDesignerHost when provided with a format context.

GetStandardValuesExclusive()

Returns whether the collection of standard values returned from GetStandardValues() is an exclusive list.

(Inherited from TypeConverter)
GetStandardValuesExclusive(ITypeDescriptorContext)

Returns a value indicating whether the collection of standard values returned by the GetStandardValues(ITypeDescriptorContext) method is an exclusive list of possible values, using the specified context.

GetStandardValuesSupported()

Returns whether this object supports a standard set of values that can be picked from a list.

(Inherited from TypeConverter)
GetStandardValuesSupported(ITypeDescriptorContext)

Returns a value indicating whether this object supports a standard set of control ID values that can be picked from a list, using the specified context.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsValid(ITypeDescriptorContext, Object)

Returns whether the given value object is valid for this type and for the specified context.

(Inherited from TypeConverter)
IsValid(Object)

Returns whether the given value object is valid for this type.

(Inherited from TypeConverter)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SortProperties(PropertyDescriptorCollection, String[])

Sorts a collection of properties.

(Inherited from TypeConverter)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also