【问题标题】:Dynamic data get current row data in field template动态数据获取字段模板中的当前行数据
【发布时间】:2015-08-18 13:10:57
【问题描述】:

我有一个动态数据网站,它有一个自定义字段模板,

CustomFieldTemplate_Edit.ascx

里面会用到

ListDetails.aspx

对于某些页面。我已经将它设置在我需要使用 UIHint 属性的地方,并且我想实现一些自定义功能。为此,我需要一种方法来获取包含我的 CustomFieldTemplate 的当前行的数据。我怎样才能做到这一点 ?

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 有一个 Row 属性,但我不知道如何使用它。当我尝试访问它时,我收到此错误:

"Eval()、XPath()、Bind()等数据绑定方法只能 在数据绑定控件的上下文中使用。"

【问题讨论】:

    标签: c# asp.net entity-framework-6 asp.net-dynamic-data


    【解决方案1】:

    我找到了解决方案here。答案是FieldTemplateUserControlRow 属性,但您必须在FieldTemplate 的OnDataBinding 事件中访问它。

    获得 Row 后,您可以使用这些扩展方法(取自上面的链接)从中获取实体,这一切都像一个魅力。

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多