C# 动态数据获取字段模板中的当前行数据
我有一个动态数据网站,它有一个自定义字段模板 CustomFieldTemplate_Edit.ascx 这将在内部使用 ListDetails.aspx 对于一些页面。我已经使用UIHint属性在需要的地方设置了它,我想实现一些自定义功能。为此,我需要一种方法来获取包含CustomFieldTemplate的当前行的数据。我该怎么做C# 动态数据获取字段模板中的当前行数据,c#,asp.net,entity-framework-6,asp.net-dynamic-data,C#,Asp.net,Entity Framework 6,Asp.net Dynamic Data,我有一个动态数据网站,它有一个自定义字段模板 CustomFieldTemplate_Edit.ascx 这将在内部使用 ListDetails.aspx 对于一些页面。我已经使用UIHint属性在需要的地方设置了它,我想实现一些自定义功能。为此,我需要一种方法来获取包含CustomFieldTemplate的当前行的数据。我该怎么做 public partial class CustomFieldTemplate_Edit : System.Web.DynamicData.FieldTempl
public partial class CustomFieldTemplate_Edit : System.Web.DynamicData.FieldTemplateUserControl
{
protected void Page_PreRender(object sender, EventArgs e)
{
//Need to get current row data, or at least the primary key here
}
}
我注意到FieldTemplateUserControl有一个行
属性,但我不知道如何使用它。当我尝试访问它时,会出现以下错误:
诸如Eval()、XPath()和Bind()之类的数据绑定方法只能是
在数据绑定控件的上下文中使用。“
我找到了解决办法。答案是FieldTemplateUserControl
的Row
属性,但您必须在FieldTemplate的OnDataBinding
事件中访问它
获取行后,可以使用这些扩展方法(从上面的链接中获取)从中获取实体,所有这些都像一个符咒
public static class EntityDataSourceExtensions
{
public static TEntity GetEntityAs<TEntity>(this object dataItem)
where TEntity : class
{
var entity = dataItem as TEntity;
if (entity != null)
return entity;
var td = dataItem as ICustomTypeDescriptor;
if (td != null)
return (TEntity)td.GetPropertyOwner(null);
return null;
}
public static Object GetEntity(this object dataItem)
{
var td = dataItem as ICustomTypeDescriptor;
if (td != null)
return td.GetPropertyOwner(null);
return null;
}
}
公共静态类EntityDataSourceExtensions
{
公共静态TEntity GetEntityAs(此对象数据项)
地点:班级
{
var实体=数据项作为可能性;
如果(实体!=null)
返回实体;
var td=作为ICustomTypeDescriptor的数据项;
如果(td!=null)
return(TEntity)td.GetPropertyOwner(null);
返回null;
}
公共静态对象GetEntity(此对象数据项)
{
var td=作为ICustomTypeDescriptor的数据项;
如果(td!=null)
返回td.GetPropertyOwner(null);
返回null;
}
}
我找到了一个解决方案。答案是FieldTemplateUserControl
的Row
属性,但您必须在FieldTemplate的OnDataBinding
事件中访问它
获取行后,可以使用这些扩展方法(从上面的链接中获取)从中获取实体,所有这些都像一个符咒
public static class EntityDataSourceExtensions
{
public static TEntity GetEntityAs<TEntity>(this object dataItem)
where TEntity : class
{
var entity = dataItem as TEntity;
if (entity != null)
return entity;
var td = dataItem as ICustomTypeDescriptor;
if (td != null)
return (TEntity)td.GetPropertyOwner(null);
return null;
}
public static Object GetEntity(this object dataItem)
{
var td = dataItem as ICustomTypeDescriptor;
if (td != null)
return td.GetPropertyOwner(null);
return null;
}
}
公共静态类EntityDataSourceExtensions
{
公共静态TEntity GetEntityAs(此对象数据项)
地点:班级
{
var实体=数据项作为可能性;
如果(实体!=null)
返回实体;
var td=作为ICustomTypeDescriptor的数据项;
如果(td!=null)
return(TEntity)td.GetPropertyOwner(null);
返回null;
}
公共静态对象GetEntity(此对象数据项)
{
var td=作为ICustomTypeDescriptor的数据项;
如果(td!=null)
返回td.GetPropertyOwner(null);
返回null;
}
}