Personas que lo han encontrado útil: 0 de 2 - Valorar este tema

DataBinding (Clase)

Actualización: noviembre 2007

Contiene información sobre una única expresión de enlace de datos en un control de servidor ASP.NET, que permite que diseñadores de desarrollo rápido de aplicaciones (RAD), como Microsoft Visual Studio, creen expresiones de enlace de datos en tiempo de diseño. Esta clase no se puede heredar.

Espacio de nombres:  System.Web.UI
Ensamblado:  System.Web (en System.Web.dll)
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class DataBinding
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public final class DataBinding
public final class DataBinding

Cada expresión de enlace a datos de un control de servidor se representa en tiempo de diseño mediante una instancia de la clase DataBinding. Cualquier control de servidor que contenga una o más expresiones de enlace a datos tiene un objeto DataBindingCollection que contiene los objetos DataBinding. Esta colección está accesible a través de la clase Control que implementa la interfaz IDataBindingsAccessor. Al crear un diseñador RAD personalizado, hay que utilizar esa implementación para obtener acceso a la colección. Los objetos DataBinding o DataBindingCollection que estén asociados a un control de servidor sólo existen en tiempo de diseño. No existen ni se puede obtener acceso a ellos en tiempo de ejecución.

En el siguiente ejemplo de código se crea un objeto DataBinding y se configura de la misma forma que un objeto existente en la colección DataBindingCollection del control, que tiene un parámetro propertyName con el valor Text. Si la colección contiene un objeto DataBinding con el valor de propertyName igual a Text, este código devuelve el valor de la propiedad Expression del objeto. Si dicho objeto no existe, devuelve una cadena vacía ("").

// Create the custom class that accesses the DataBinding and
// DataBindingCollection classes at design time.
public class SimpleDesigner : System.Web.UI.Design.ControlDesigner
{
    // Create a Text property with accessors that obtain 
    // the property value from and set the property value
    // to the Text key in the DataBindingCollection class.
    public string Text
    {
        get
        {
            DataBinding myBinding = DataBindings["Text"];
            if (myBinding != null)
            {
                return myBinding.Expression;
            }
            return String.Empty;
        }
        set
        {

            if ((value == null) || (value.Length == 0))
            {
                DataBindings.Remove("Text");
            }
            else
            {

                DataBinding binding = DataBindings["Text"];

                if (binding == null)
                {
                    binding = new DataBinding("Text", typeof(string), value);
                }
                else
                {
                    binding.Expression = value;
                }
                // Call the DataBinding constructor, then add
                // the initialized DataBinding object to the 
                // DataBindingCollection for this custom designer.
                DataBinding binding1 = (DataBinding)DataBindings.SyncRoot;
                DataBindings.Add(binding);
                DataBindings.Add(binding1);
            }
            PropertyChanged("Text");
        }
    }
    protected void PropertyChanged(string propName)
    {
        IControlDesignerTag myHtmlControlDesignBehavior = this.Tag;

        DataBindingCollection myDataBindingCollection;
        DataBinding myDataBinding1, myDataBinding2;
        String myStringReplace1, myDataBindingExpression1, removedBinding, removedBindingAfterReplace, myDataBindingExpression2, myStringReplace2;
        string[] removedBindings1, removedBindings2;
        Int32 temp;

        if (myHtmlControlDesignBehavior == null)
            return;
        // Use the DataBindingCollection constructor to 
        // create the myDataBindingCollection1 object.
        // Then set this object equal to the
        // DataBindings property of the control created
        // by this custom designer.
        DataBindingCollection myDataBindingCollection1 = new DataBindingCollection();
        myDataBindingCollection1 = myDataBindingCollection = DataBindings;
        if (myDataBindingCollection.Contains(propName))
        {
            myDataBinding1 = myDataBindingCollection[propName];
            myStringReplace1 = propName.Replace(".", "-");
            if (myDataBinding1 == null)
            {
                myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1);
                return;
            }
            // DataBinding is not null.
            myDataBindingExpression1 = String.Concat("<%#", myDataBinding1.Expression, "%>");
            myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, myDataBindingExpression1);
            int index = myStringReplace1.IndexOf("-");
        }
        else
        {
            // Use the DataBindingCollection.RemovedBindings 
            // property to set the value of the removedBindings
            // arrays.
            removedBindings2 = removedBindings1 = DataBindings.RemovedBindings;
            temp = 0;
            while (removedBindings2.Length > temp)
            {
                removedBinding = removedBindings2[temp];
                removedBindingAfterReplace = removedBinding.Replace('.', '-');
                myHtmlControlDesignBehavior.RemoveAttribute(removedBindingAfterReplace);
                temp = temp + 1;
            }
        }
        // Use the DataBindingCollection.GetEnumerator method
        // to iterate through the myDataBindingCollection object
        // and write the PropertyName, PropertyType, and Expression
        // properties to a file for each DataBinding object
        // in the MyDataBindingCollection object. 
        myDataBindingCollection = DataBindings;
        IEnumerator myEnumerator = myDataBindingCollection.GetEnumerator();

        while (myEnumerator.MoveNext())
        {
            myDataBinding2 = (DataBinding)myEnumerator.Current;
            String dataBindingOutput1, dataBindingOutput2, dataBindingOutput3;
            dataBindingOutput1 = String.Concat("The property name is ", myDataBinding2.PropertyName);
            dataBindingOutput2 = String.Concat("The property type is ", myDataBinding2.PropertyType.ToString(), "-", dataBindingOutput1);
            dataBindingOutput3 = String.Concat("The expression is ", myDataBinding2.Expression, "-", dataBindingOutput2);
            WriteToFile(dataBindingOutput3);

            myDataBindingExpression2 = String.Concat("<%#", myDataBinding2.Expression, "%>");
            myStringReplace2 = myDataBinding2.PropertyName.Replace(".", "-");
            myHtmlControlDesignBehavior.SetAttribute(myStringReplace2, myDataBindingExpression2);
            int index = myStringReplace2.IndexOf('-');
        }// while loop ends
    }
    public void WriteToFile(string input)
    {
        // The WriteToFile custom method writes
        // the values of the DataBinding properties
        // to a file on the C drive at design time.
        StreamWriter myFile = File.AppendText("C:\\DataBindingOutput.txt");
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] ByteArray = encoder.GetBytes(input);
        char[] CharArray = encoder.GetChars(ByteArray);
        myFile.WriteLine(CharArray, 0, input.Length);
        myFile.Close();
    }
}


// Create the custom class that accesses the DataBinding and
// DataBindingCollection classes at design time.
public class SimpleDesigner extends System.Web.UI.Design.ControlDesigner
{
    // Create a Text property with accessors that obtain 
    // the property value from and set the property value
    // to the Text key in the DataBindingCollection class.
    /** @property 
     */
    public String get_Text()
    {
        DataBinding myBinding = get_DataBindings().get_Item("Text");
        if (myBinding != null) {
            return myBinding.get_Expression();
        }
        return("");
    } //get_Text

    /** @property 
     */
    public void set_Text(String value)
    {
        if (value == null || value.get_Length() == 0) {
            get_DataBindings().Remove("Text");
        }
        else {
            DataBinding binding = get_DataBindings().get_Item("Text");
            if (binding == null) {
                binding = new DataBinding("Text", String.class.ToType(), value);
            }
            else {
                binding.set_Expression(value);
            }

            // Call the DataBinding constructor, then add
            // the initialized DataBinding object to the 
            // DataBindingCollection for this custom designer.
            DataBinding binding1 = (DataBinding)(get_DataBindings().
                get_SyncRoot());
            get_DataBindings().Add(binding);
            get_DataBindings().Add(binding1);
        }
        OnBindingsCollectionChanged("Text");
    } //set_Text

    // Override the OnBindingsCollectionChanged class to create
    // the data-binding expression and associate it with 
    // a property on the control created by the designer.
    protected void OnBindingsCollectionChanged(String propName)
    {
        IHtmlControlDesignerBehavior myHtmlControlDesignBehavior = 
            get_Behavior();
        DataBindingCollection myDataBindingCollection;
        DataBinding myDataBinding1, myDataBinding2;
        String myStringReplace1, myDataBindingExpression1, removedBinding, 
        removedBindingAfterReplace, myDataBindingExpression2, myStringReplace2;
        String removedBindings1[], removedBindings2[];
        int temp;

        if (myHtmlControlDesignBehavior == null) {
            return;
        }

        // Use the DataBindingCollection constructor to 
        // create the myDataBindingCollection1 object.
        // Then set this object equal to the
        // DataBindings property of the control created
        // by this custom designer.
        DataBindingCollection myDataBindingCollection1 = 
            new DataBindingCollection();
        myDataBindingCollection1 = (myDataBindingCollection = 
            get_DataBindings());

        if (propName != null) {
            myDataBinding1 = myDataBindingCollection.get_Item(propName);
            myStringReplace1 = propName.Replace(".", "-");
            if (myDataBinding1 == null) {
                myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1, 
                    true);
                return;
            }

            // DataBinding is not null.
            myDataBindingExpression1 = String.Concat("<%#", myDataBinding1.
                get_Expression(), "%>");
            myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, 
                myDataBindingExpression1, true);
            int index = myStringReplace1.IndexOf("-");
        }
        else {
            // Use the DataBindingCollection.RemovedBindings 
            // property to set the value of the removedBindings
            // arrays.
            removedBindings2 = (removedBindings1 = get_DataBindings().
                get_RemovedBindings());

            temp = 0;
            while (removedBindings2.length > temp) {
                removedBinding = removedBindings2[temp];
                removedBindingAfterReplace = removedBinding.Replace('.', '-');
                myHtmlControlDesignBehavior.RemoveAttribute(
                    removedBindingAfterReplace, true);
                temp = temp + 1;
            }
        }

        // Use the DataBindingCollection.GetEnumerator method
        // to iterate through the myDataBindingCollection object
        // and write the PropertyName, PropertyType, and Expression
        // properties to a file for each DataBinding object
        // in the MyDataBindingCollection object. 
        myDataBindingCollection = get_DataBindings();
        IEnumerator myEnumerator = myDataBindingCollection.GetEnumerator();
        while (myEnumerator.MoveNext()) {
            myDataBinding2 = (DataBinding)(myEnumerator.get_Current());
            String dataBindingOutput1, dataBindingOutput2, dataBindingOutput3;
            dataBindingOutput1 = String.Concat("The property name is ", 
                myDataBinding2.get_PropertyName());
            dataBindingOutput2 = String.Concat("The property type is ", 
                myDataBinding2.get_PropertyType().ToString(), "-", 
                dataBindingOutput1);
            dataBindingOutput3 = String.Concat("The expression is ", 
                myDataBinding2.get_Expression(), "-", dataBindingOutput2);
            WriteToFile(dataBindingOutput3);
            myDataBindingExpression2 = String.Concat("<%#", myDataBinding2.
                get_Expression(), "%>");
            myStringReplace2 = myDataBinding2.get_PropertyName().
                Replace(".", "-");
            myHtmlControlDesignBehavior.SetAttribute(myStringReplace2, 
                myDataBindingExpression2, true);
            int index = myStringReplace2.IndexOf("-");
        } // while loop ends
    } //OnBindingsCollectionChanged

    public void WriteToFile(String input)
    {
        // The WriteToFile custom method writes
        // the values of the DataBinding properties
        // to a file on the C drive at design time.
        StreamWriter myFile = File.AppendText("C:\\DataBindingOutput.txt");
        ASCIIEncoding encoder = new ASCIIEncoding();
        ubyte byteArray[] = encoder.GetBytes(input);
        char charArray[] = encoder.GetChars(byteArray);
        myFile.WriteLine(charArray, 0, input.get_Length());
        myFile.Close();
    } //WriteToFile


System.Object
  System.Web.UI.DataBinding
Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0, 1.1, 1.0
¿Te ha resultado útil?
(Caracteres restantes: 1500)

Adiciones de comunidad

AGREGAR
© 2013 Microsoft. Reservados todos los derechos.