Export (0) Print
Expand All

ControlIDConverter Class

Note: This class is new in the .NET Framework version 2.0.

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

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class ControlIDConverter : StringConverter
public class ControlIDConverter extends StringConverter
public class ControlIDConverter extends StringConverter

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.

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 DebugInfoControloverrides 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>");

      }
    }
  }
}

package Samples.AspNet.JSL ;

import System.*;
import System.ComponentModel.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;

/** @attribute DefaultProperty("ControlID")
 */
public class DebugInfoControl extends Control
{
    public DebugInfoControl()
    {
    } //DebugInfoControl

    public DebugInfoControl(String controlID)
    {
        set_ControlID(controlID);
    } //DebugInfoControl

    /** @attribute DefaultValue("")
     *  @attribute TypeConverter(ControlIDConverter.class)
     */
    /** @property 
     */
    public String get_ControlID()
    {
        Object o = get_ViewState().get_Item("ControlID");
        if (o == null) {
            return "";
        }
        return (String)(o);
    }//get_ControlID

    /** @property 
     */
    public void set_ControlID(String value)
    {
        if (!(get_ControlID().Equals(value))) {
            get_ViewState().set_Item("ControlID", value);
        }
    }//set_ControlID

    protected void Render(HtmlTextWriter writer)
    {
        Label info = new Label();
        if (this.get_ControlID().length() == 0) {
            writer.Write("<Font Color='Red'>No ControlID set.</Font>");
        }

        Control ctrl = this.FindControl(get_ControlID());

        if (ctrl == null) {
            writer.Write(("<Font Color='Red'>Could not find control " 
                + get_ControlID() + " in Naming Container.</Font>"));
        }
        else {
            writer.Write(("<Font Color='Green'>Control " + get_ControlID()
                + " found.<BR>"));
            writer.Write(("Its Naming Container is: " +
                ctrl.get_NamingContainer().get_ID() + "<BR>"));

            if (ctrl.get_EnableViewState()) {
                writer.Write("It uses view state to persist its state.<BR>");
            }

            if (ctrl.get_EnableTheming()) {
                writer.Write("It can be assigned a Theme ID.<BR>");
            }

            if (ctrl.get_Visible()) {
                writer.Write("It is visible on the page.<BR>");
            }
            else {
                writer.Write("It is not visible on the page.<BR>");
            }
            writer.Write("</Font>");
        }
    } //Render
} //DebugInfoControl   

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>
  <BODY>
    <FORM runat="server">

      <asp:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datasourcemode="DataReader"
          datafile="Northwind.mdb"
          selectcommand="SELECT OrderID FROM Orders WHERE EmployeeID=2">
      </asp:accessdatasource>

      <p>
      <aspSample:debuginfocontrol
          id="DebugInfoControl1"
          runat="server"
          controlid="AccessDataSource1" />

    </FORM>
  </BODY>
</HTML>

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" assembly="Samples.AspNet.JSL" %>
<%@Page  Language="VJ#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
  <BODY>
    <FORM runat="server">

      <asp:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datasourcemode="DataReader"
          datafile="Northwind.mdb"
          SelectCommand = "SELECT OrderID
            FROM Orders
            WHERE EmployeeID=2">          
      </asp:accessdatasource>

      <p>
      <aspSample:debuginfocontrol
          id="DebugInfoControl1"
          runat="server"
          controlid="AccessDataSource1" />

    </FORM>
  </BODY>
</HTML>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft