DataBinding Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Contiene informazioni su una singola espressione di associazione dati in un controllo server ASP.NET che consentono alle finestre di progettazione RAD, quali Microsoft Visual Studio, di creare espressioni di associazione dati in fase di progettazione. La classe non può essere ereditata.
public ref class DataBinding sealed
public sealed class DataBinding
type DataBinding = class
Public NotInheritable Class DataBinding
- Ereditarietà
-
DataBinding
Esempio
Nell'esempio di codice seguente viene creato un DataBinding oggetto e viene impostato come uguale a un oggetto esistente nell'insieme del DataBindingCollection controllo con un propertyName
parametro con valore Text
. Se l'insieme contiene un DataBinding oggetto con valore propertyName
Text
, questo codice restituisce il valore della proprietà dell'oggetto Expression . Se non esiste un oggetto di questo tipo, restituisce una stringa vuota ("").
// 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
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(ByVal value As String)
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
PropertyChanged("Text")
End Set
End Property
Protected Sub PropertyChanged(ByVal propName As String)
Dim myHtmlControlDesignBehavior As IControlDesignerTag = Me.Tag
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 (myDataBindingCollection.Contains(propName)) Then
myDataBinding1 = myDataBindingCollection(propName)
myStringReplace1 = propName.Replace(".", "-")
If myDataBinding1 Is Nothing Then
myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1)
Return
End If
' DataBinding is not null.
myDataBindingExpression1 = [String].Concat("<%#", myDataBinding1.Expression, "%>")
myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, myDataBindingExpression1)
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)
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)
Dim index As Integer = myStringReplace2.IndexOf("-"c)
End While ' while loop ends
End Sub
Public Sub WriteToFile(ByVal 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
End Class
Commenti
Ogni espressione di data binding in un controllo server è rappresentata in fase di progettazione da un'istanza della DataBinding classe . Qualsiasi controllo server che contiene una o più espressioni di data binding dispone di un DataBindingCollection oggetto che contiene gli DataBinding oggetti . Questa raccolta è accessibile tramite la Control classe che implementa l'interfaccia IDataBindingsAccessor . Quando si crea una finestra di progettazione RAD personalizzata, usare tale implementazione per accedere alla raccolta. Tutti DataBinding gli oggetti o DataBindingCollection associati a un controllo server esistono solo in fase di progettazione. Non esistono in fase di esecuzione e pertanto non sono accessibili durante l'esecuzione.
Costruttori
DataBinding(String, Type, String) |
Inizializza una nuova istanza della classe DataBinding. |
Proprietà
Expression |
Ottiene o imposta l'espressione di associazione dati da valutare. |
PropertyName |
Ottiene il nome della proprietà del controllo server ASP.NET alla quale associare i dati. |
PropertyType |
Ottiene il tipo .NET Framework della proprietà del controllo server ASP.NET con associazione a dati. |
Metodi
Equals(Object) |
Determina se l'oggetto specificato è l'istanza della classe DataBinding corrispondente all'oggetto corrente. |
GetHashCode() |
Recupera il codice hash di un'istanza dell'oggetto DataBinding. |
GetType() |
Ottiene l'oggetto Type dell'istanza corrente. (Ereditato da Object) |
MemberwiseClone() |
Crea una copia superficiale dell'oggetto Object corrente. (Ereditato da Object) |
ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |