Using DataObjectTypeName in a DataObjectSource

The functionality I'm trying to use is: - Create an ObjectDataSource object to select and update controls on a web page (User Control). - Use DataObjectTypeName to create an object that sends data to the UpdateMethod method. - Before the values ​​are filled in the DataObjectTypeNames object, I would like to pre-populate the object so that unused elements in the class are not defaulted to zero and empty strings by default, not knowing whether the user specified a zero or a standard string using the application.

I can’t find a way to pre-populate the values ​​(this was a problem back in 2006 with the 2.0 framework). One may ask: "Why would anyone need to pre-populate an object?" The simple answer is: I want to be able to randomly place controls on different User Controls and don’t have to worry about which UpdateMethod should process which fields of the object.

As an example, let's say that I have a class (which reflects the SQL table) that includes the fields: FirstName, LastName, Address, City, State, Zip. Perhaps I want to give the user the opportunity to change the names of FirstName and LastName and not even see the address, city, state, zip code (or vice versa). I do not want to create two UpdateMethod methods where FirstName and LastName are processed, and the other method handles other fields. I work with a class of more than 40 columns from several tables, and I may need some fields on one screen and not another, and later decide to change these fields from one screen to another (which violates my UpdateMethod methods without my knowledge).

I hope I have explained my problem well enough.
Thanks

+3
1

, .

GridView DataSourceID, ObjectDataSource.

, , , - - , GridView.

:

public static class GridViewExtensions
{
    public static void EnableLimitUpdateToGridViewColumns(this GridView gridView)
    {
        _gridView = gridView;
        if (_gridView.DataSourceObject != null)
        {
            ((ObjectDataSource)_gridView.DataSourceObject)
                .Updating += new ObjectDataSourceMethodEventHandler(objectDataSource_Updating);
        }
    }

    private static GridView _gridView;

    private static void objectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
        var newObject = ((object)e.InputParameters[0]);
        var oldObjects = ((ObjectDataSource)_gridView.DataSourceObject).Select().Cast<object>();

        Type type = oldObjects.First().GetType();

        object oldObject = null;

        foreach (var obj in oldObjects)
        {
            if (type.GetProperty(_gridView.DataKeyNames.First()).GetValue(obj, null).ToString() ==
                type.GetProperty(_gridView.DataKeyNames.First()).GetValue(newObject, null).ToString())
            {
                oldObject = obj;
                break;
            }
        }

        if (oldObject == null) return;

        var dynamicColumns = _gridView.Columns.OfType<DynamicField>();

        foreach (var property in type.GetProperties())
        {
            if (dynamicColumns.Where(c => c.DataField == property.Name).Count() == 0)
            {
                property.SetValue(newObject, property.GetValue(oldObject, null), null);
            }
        }
    }
}

Page_Init GridView, :

protected void Page_Init()
{
    GridView1.EnableLimitUpdateToGridViewColumns();
}

.

, , . ListView DetailsView.

, , -, .. .

, GridView ObjectDataSource, .

0

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


All Articles