3

I'm modifying the "Edit.aspx" default page template used by ASP.NET Dynamic Data and adding some additional controls. I know that I can find the type of object being edited by looking at DetailsDataSource.GetTable().EntityType, but how can I see the actual object itself? Also, can I change the properties of the object and tell the data context to submit those changes?

4

3 回答 3

1

也许您已经找到了解决方案,但是我想分享我对此的经验。

结果证明这是一个很棒的皮塔饼,但我设法获得了编辑行。我必须提取 DetailsDataSource WhereParameters,然后在运行时创建查询。

下面的代码适用于具有单个主键的表。如果你有复合键,我猜,它需要修改:

Parameter param = null;
foreach(object item in (DetailsDataSource.WhereParameters[0] as DynamicQueryStringParameter).GetWhereParameters(DetailsDataSource)) {
    param = (Parameter)item;
    break;
}

IQueryable query = DetailsDataSource.GetTable().GetQuery();
ParameterExpression lambdaArgument = Expression.Parameter(query.ElementType, "");
object paramValue = Convert.ChangeType(param.DefaultValue, param.Type);
Expression compareExpr = Expression.Equal(
    Expression.Property(lambdaArgument, param.Name),
    Expression.Constant(paramValue)
);
Expression lambda = Expression.Lambda(compareExpr, lambdaArgument);
Expression filteredQuery = Expression.Call(typeof(Queryable), "Where", new Type[] { query.ElementType }, query.Expression, lambda);
var WANTED = query.Provider.CreateQuery(filteredQuery).Cast<object>().FirstOrDefault<object>();
于 2009-03-18T11:13:21.740 回答
0

如果它是 DD 对象,您可以使用 FieldTemplateUserControl.FindFieldTemplate(controlId)。然后,如果需要,可以将其转换为 ITextControl 来操作数据。

否则,请尝试使用此扩展方法查找子控件:

    public static T FindControl<T>(this Control startingControl, string id) where T : Control
    {
        T found = startingControl.FindControl(id) as T;

        if (found == null)
        {
            found = FindChildControl<T>(startingControl, id);
        }

        return found;
    }
于 2009-06-12T23:28:07.137 回答
0

我找到了另一个解决方案,其他的都不起作用。
就我而言,我已将 Edit.aspx 复制到 /CustomPages/Devices/中,
其中Devices是我想要此自定义行为的表的名称。

将此添加到 Edit.aspx -> Page_Init()

DetailsDataSource.Selected += entityDataSource_Selected;

在 Edit.aspx 中添加:

protected void entityDataSource_Selected(object sender, EntityDataSourceSelectedEventArgs e)
{
    Device device = e.Results.Cast<Device>().First();       
    // you have the object/row being edited !
}

只需将 Device 更改为您自己的表名。

于 2015-07-24T02:42:00.493 回答