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 Visual Studio .NET, to create data-binding expressions at design time. This class cannot be inherited.
For a list of all members of this type, see DataBinding Members.
System.Object
System.Web.UI.DataBinding
[Visual Basic] NotInheritable Public Class DataBinding [C#] public sealed class DataBinding [C++] public __gc __sealed class DataBinding [JScript] public class DataBinding
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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 implemention 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.
Example
[Visual Basic, C#, C++] The following example creates a DataBinding object and sets it equal to an existing object in the control's DataBindingCollection 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.
[Visual Basic] ' Create the custom class that accesses the DataBinding and ' DataBindingCollection classes at design time. Public Class SimpleDesigner Inherits 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 Property [Text]() As String Get Dim myBinding As DataBinding = DataBindings("Text") If Not (myBinding Is Nothing) Then Return myBinding.Expression End If Return String.Empty End Get Set If value Is Nothing OrElse value.Length = 0 Then DataBindings.Remove("Text") Else Dim binding As DataBinding = DataBindings("Text") If binding Is Nothing Then binding = New DataBinding("Text", GetType(String), value) Else binding.Expression = value End If ' Call the DataBinding constructor, then add ' the initialized DataBinding object to the ' DataBindingCollection for this custom designer. Dim binding1 As DataBinding = CType(DataBindings.SyncRoot, DataBinding) DataBindings.Add(binding) DataBindings.Add(binding1) End If OnBindingsCollectionChanged("Text") End Set End Property ' Override the OnBindingsCollectionChanged class to create ' the data-binding expression and associate it with ' a property on the control created by the designer. Protected Overrides Sub OnBindingsCollectionChanged(propName As String) Dim myHtmlControlDesignBehavior As IHtmlControlDesignerBehavior = Behavior Dim myDataBindingCollection As DataBindingCollection Dim myDataBinding1, myDataBinding2 As DataBinding Dim myStringReplace1, myDataBindingExpression1, removedBinding, removedBindingAfterReplace, myDataBindingExpression2, myStringReplace2 As [String] Dim removedBindings1(), removedBindings2() As String Dim temp As Int32 If myHtmlControlDesignBehavior Is Nothing Then Return End If myDataBindingCollection = DataBindings ' 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. Dim myDataBindingCollection1 As New DataBindingCollection() myDataBindingCollection1 = DataBindings myDataBindingCollection = DataBindings If Not (propName Is Nothing) Then myDataBinding1 = myDataBindingCollection(propName) myStringReplace1 = propName.Replace(".", "-") If myDataBinding1 Is Nothing Then myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1, True) Return End If ' DataBinding is not null. myDataBindingExpression1 = [String].Concat("<%#", myDataBinding1.Expression, "%>") myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, myDataBindingExpression1, True) Dim index As Integer = myStringReplace1.IndexOf("-") Else ' Use the DataBindingCollection.RemovedBindings ' property to set the value of the removedBindings ' arrays. removedBindings1 = DataBindings.RemovedBindings removedBindings2 = DataBindings.RemovedBindings temp = 0 While removedBindings2.Length > temp removedBinding = removedBindings2(temp) removedBindingAfterReplace = removedBinding.Replace("."c, "-"c) myHtmlControlDesignBehavior.RemoveAttribute(removedBindingAfterReplace, True) temp = temp & 1 End While End If ' 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 Dim myEnumerator As IEnumerator = myDataBindingCollection.GetEnumerator() While myEnumerator.MoveNext() myDataBinding2 = CType(myEnumerator.Current, DataBinding) Dim dataBindingOutput1, dataBindingOutput2, dataBindingOutput3 As [String] 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, True) Dim index As Integer = myStringReplace2.IndexOf("-"c) End While ' while loop ends End Sub 'OnBindingsCollectionChanged Public Sub WriteToFile(input As String) ' The WriteToFile custom method writes ' the values of the DataBinding properties ' to a file on the C drive at design time. Dim myFile As StreamWriter = File.AppendText("C:\DataBindingOutput.txt") Dim encoder As New ASCIIEncoding() Dim ByteArray As Byte() = encoder.GetBytes(input) Dim CharArray As Char() = encoder.GetChars(ByteArray) myFile.WriteLine(CharArray, 0, input.Length) myFile.Close() End Sub 'WriteToFile End Class 'SimpleDesigner [C#] // 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); } OnBindingsCollectionChanged("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 override void OnBindingsCollectionChanged(string propName) { IHtmlControlDesignerBehavior myHtmlControlDesignBehavior = Behavior; 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 (propName != null) { myDataBinding1 = myDataBindingCollection[propName]; myStringReplace1 = propName.Replace(".","-"); if (myDataBinding1 == null) { myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1,true); return; } // DataBinding is not null. myDataBindingExpression1 = String.Concat("<%#",myDataBinding1.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 = DataBindings.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 = 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,true); 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(); } } [C++] // Create the custom class that accesses the DataBinding and // DataBindingCollection classes at design time. public __gc class SimpleDesigner : public 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: __property String* get_Text() { DataBinding* myBinding = DataBindings->Item[S"Text"]; if (myBinding != 0) { return myBinding->Expression; } return String::Empty; } __property void set_Text(String* value) { if ((value == 0) || (value->Length == 0)) { DataBindings->Remove(S"Text"); } else { DataBinding* binding = DataBindings->Item[S"Text"]; if (binding == 0) { binding = new DataBinding(S"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=dynamic_cast<DataBinding*>(DataBindings->SyncRoot); DataBindings->Add(binding); DataBindings->Add(binding1); } OnBindingsCollectionChanged(S"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 = Behavior; DataBindingCollection* myDataBindingCollection; DataBinding* myDataBinding1, * myDataBinding2; String* myStringReplace1; String* myDataBindingExpression1; String* removedBinding; String* removedBindingAfterReplace; String* myDataBindingExpression2; String* myStringReplace2; String* removedBindings1[]; String* removedBindings2[]; Int32 temp; if (myHtmlControlDesignBehavior == 0) 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 (propName != 0) { myDataBinding1 = myDataBindingCollection->Item[propName]; myStringReplace1 = propName->Replace(S".", S"-"); if (myDataBinding1 == 0) { myHtmlControlDesignBehavior->RemoveAttribute(myStringReplace1, true); return; } // DataBinding is not 0. myDataBindingExpression1 = String::Concat(S"<%#", myDataBinding1->Expression, S"%>"); myHtmlControlDesignBehavior->SetAttribute(myStringReplace1, myDataBindingExpression1, true); int index = myStringReplace1->IndexOf(S"-"); } 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, 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 = DataBindings; IEnumerator* myEnumerator = myDataBindingCollection->GetEnumerator(); while(myEnumerator->MoveNext()) { myDataBinding2 = dynamic_cast<DataBinding*>(myEnumerator->Current); String* dataBindingOutput1, *dataBindingOutput2, *dataBindingOutput3; dataBindingOutput1=String::Concat(S"The property name is ", myDataBinding2->PropertyName); dataBindingOutput2=String::Concat(S"The property type is ", myDataBinding2->PropertyType, S"-", dataBindingOutput1); dataBindingOutput3=String::Concat(S"The expression is ", myDataBinding2->Expression, S"-", dataBindingOutput2); WriteToFile(dataBindingOutput3); myDataBindingExpression2 = String::Concat(S"<%#", myDataBinding2->Expression, S"%>"); myStringReplace2 = myDataBinding2->PropertyName->Replace(S".", S"-"); myHtmlControlDesignBehavior->SetAttribute(myStringReplace2, myDataBindingExpression2, true); 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(S"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(); } };
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
DataBinding Members | System.Web.UI Namespace | DataBinder | DataBindingCollection | IDataBindingsAccessor