This documentation is archived and is not being maintained.

DataBinding Class

Contains information about a single data-binding expression in an ASP.NET server control, which allows rapid-application development (RAD) designers, such as Microsoft Visual Studio, to create data-binding expressions at design time. This class cannot be inherited.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class DataBinding

Each data-binding expression in a server control is represented at design time by an instance of the DataBinding class. Any server control that contains one or more data-binding expressions has a DataBindingCollection object that contains the DataBinding objects. This collection is accessible through the Control class implementing the IDataBindingsAccessor interface. When you create a custom RAD designer, use that implementation to access the collection. Any DataBinding or DataBindingCollection objects associated with a server control exist only at design time. They do not exist at run time and, therefore, are not accessible during run time.

The following code example creates a DataBinding object and sets it equal to an existing object in the control's DataBindingCollection collection that has a propertyName parameter with a value of Text. If the collection contains a DataBinding object with a propertyName value of Text, this code returns the value of the object's Expression property. If there is no such object, it returns an empty string ("").

// 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
            DataBinding myBinding = DataBindings["Text"];
            if (myBinding != null)
                return myBinding.Expression;
            return String.Empty;

            if ((value == null) || (value.Length == 0))

                DataBinding binding = DataBindings["Text"];

                if (binding == null)
                    binding = new DataBinding("Text", typeof(string), value);
                    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;
    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)
        // 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)
            // DataBinding is not null.
            myDataBindingExpression1 = String.Concat("<%#", myDataBinding1.Expression, "%>");
            myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, myDataBindingExpression1);
            int index = myStringReplace1.IndexOf("-");
            // 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('.', '-');
                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);

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


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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0