Access to an editable object / row in dynamic data

I am modifying the default "Edit.aspx" page template used by ASP.NET dynamic data and adding some additional controls. I know that I can find the type of editable object by looking at DetailsDataSource.GetTable().EntityType, but how can I see the object itself? Also, can I change the properties of an object and tell the data context these changes?

+3
source share
3 answers

You may have found a solution already, but I would like to share my experience with this.

It turned out that this is a great lavash, but I managed to get the editing line. I had to fetch DetailsDataSource WhereParameters and then create a query at runtime.

Below is the code for tables with one primary key. If you have composite keys, I think this will require changes:

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>();
+1
source

If it is a DD object, you can use FieldTemplateUserControl.FindFieldTemplate (controlId). Then, if you need to, you can use it as an ITextControl to manage data.

Otherwise, try using this extension method to find the child control:

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

I found another solution, others did not work.
In my case, I copied Edit.aspx to / CustomPages / Devices /
Where Devices is the name of the table for which I want this behavior.

Add this to Edit.aspx -> Page_Init ()

DetailsDataSource.Selected += entityDataSource_Selected;

Add this to Edit.aspx:

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

Just change Device to your own table name.

0
source

Source: https://habr.com/ru/post/1704234/


All Articles