Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [246]

By Root 5530 0

PropertyName="SelectedValue" />

The preceding ObjectDataSource control saves rows through the Save method, which takes an Employee object. Note that when you set the DataObjectTypeName property, the UpdateParameters collection is ignored. The ObjectDataSource instantiates a default instance of the object before the operation is performed and then attempts to fill its public members with the values of any matching input fields found around the bound control. Because this work is performed using reflection, the names of the input fields in the bound control must match the names of public properties exposed by the object in the DataObjectTypeName property. A practical limitation you must be aware of is the following: you can’t define the Employee class using complex data types, as follows:

public class Employee {

public string LastName { get; set; }

public string FirstName { get; set; }

...

public Address HomeAddress {...}

}

Representing individual values (strings in the sample), the LastName and FirstName members have good chances to match an input field in the bound control. The same can’t be said for the HomeAddress member, which is declared with a custom aggregate type such as Address. If you go with this schema, all the members in Address will be ignored; any related information won’t be carried into the Save method, with resulting null parameters. All the members in the Address data structure should become members of the Employee class.

Unlike the insert operation, the update operation also requires a primary key value to uniquely identify the record being updated. If you use an explicit parameter listing, you just append an additional parameter to the list to represent the ID, as follows:

TypeName="DAL.SimpleBusinessObject"

SelectMethod="GetEmployees"

UpdateMethod="SetEmployee">

Note that by setting the DefaultValue attribute to null, you can make a parameter optional. A null value for a parameter must then be gracefully handled by the business object method that implements the update.

There’s an alternative method to set the primary key—through the DataKeyNames property of GridView and DetailsView controls. I’ll briefly mention it here and cover it in much greater detail in the next two chapters:

DataKeyNames="employeeid"

DataSourceId="MyObjectSource"

AutoGenerateEditButton="true">

...

When DataKeyNames is set on the bound control, data source controls automatically add a parameter to the list of parameters for update and delete commands. The default name of the parameter is original_XXX, where XXX stands for the value of DataKeyNames. For the operation to succeed, the method (or the SQL command if you’re using SqlDataSource) must handle a parameter with the same name. Here’s an example:

UPDATE employees SET lastname=@lastname

WHERE employeeid=@original_employeeid

The name format of the key parameter can be changed at will through the OldValuesParameterFormatString property. For example, a value of ‘{0}’ assigned to the property would make the following command acceptable:

UPDATE employees SET lastname=@lastname

WHERE employeeid=@employeeid

Setting the DataKeyNames property on the bound control (hold on, note that it’s not a property on the data source control) is also the simplest way to configure a delete operation. For a delete operation, in fact, you don’t need to specify a whole record with all its fields; the key is sufficient.

Configuring Parameters at Runtime


When using ObjectDataSource with an ASP.NET made-to-measure control (for example, GridView), most

Return Main Page Previous Page Next Page

®Online Book Reader