首先,ObjectDataSource通常用於ASP.NET WebForms(aspx)。 ObjectDataSource控件位於System.Web.UI.WebControls,因爲你可以看到在MSDN庫鏈接:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspx
使用ObjectDataSource控件綁定你的數據意味着你綁定你有數據源爲對象,可以以DataSet或實現IEnumerable的任何其他.NET對象的形式。使用ObjectDataSource意味着您必須執行通常在SqlDataSource中找到的自己的Select,Update,Insert和Delete方法。
有這個漂亮的演練在MSDN庫:Walkthrough: Data Binding to a Custom Business Object
但是,如果沒有實現IListSource綁定到一個簡單的IEnumerable(如表具有)意味着你不會有很好的功能,如數據綁定到一個複雜的數據控制,如網格視圖。而且你也會失去其他特性,因爲單獨一個簡單的IEnumerable不能以兩種方式綁定到其他列表控件,如ListView和GridView。
要使數據可以雙向綁定,您的對象在添加到IListSource中作爲數據項之前還必須實現INotifyPropertyChanged接口。
樣品:
public class Employee : BusinessObjectBase
{
private string _id;
private string _name;
private Decimal parkingId;
public Employee() : this(string.Empty, 0) {}
public Employee(string name) : this(name, 0) {}
public Employee(string name, Decimal parkingId) : base()
{
this._id = System.Guid.NewGuid().ToString();
// Set values
this.Name = name;
this.ParkingID = parkingId;
}
public string ID
{
get { return _id; }
}
const string NAME = "Name";
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
// Raise the PropertyChanged event.
OnPropertyChanged(NAME);
}
}
}
const string PARKING_ID = "Salary";
public Decimal ParkingID
{
get { return parkingId; }
set
{
if (parkingId != value)
{
parkingId = value;
// Raise the PropertyChanged event.
OnPropertyChanged(PARKING_ID);
}
}
}
}
這是INotifyPropertyChanged的實施:
public class BusinessObjectBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (null != PropertyChanged)
{
PropertyChanged(this, e);
}
}
#endregion
}
你有沒有發現你的解決方案/回答?你應該標記一個答案,或者更好的是,評論你是否仍然覺得沒有答案。 –