在订制 ASP.NET Dynamic Data 时,需要在Edit 页面做些特殊处理。如何获取当前绑定的 Entity
protected void FormView1_DataBound(object sender, EventArgs e) { ICustomTypeDescriptor item = this.FormView1.DataItem as ICustomTypeDescriptor; if (item == null) return; object entity = item.GetPropertyOwner(null); }
DataItem 的 Type 是 System.Web.UI.WebControls.EntityDataSourceWrapper,此类型为Internal 的,所以你无法访问。但此类型实现了 ICustomTypeDescriptor Public 接口。
我们可以使用此接口获取实体
具体方法就是 使用GetPropertyOwner