Monday, March 26, 2012

DetailsView & ObjectDataSource: Cancel Insert but Retain User Input

Hi,

I need to cancel an insert operation during a DetailsView/ObjectDataSource insert event, and display to the user the same insert page with their previously entered values. For example, after a server side input validation the user may need to modify some of the values they entered.

Canceling insert is easy in the ObjectDataSource.Inserting event handler: e.Cancel = true;

However, this cancels insert mode and the values the user input are lost. The ObjectDataSource.Inserting event handler has access to the input parameters, which contain the values the user has entered. It is possible to put the DetailsView object back into insert mode: DetailsView1.DefaultMode = DetailsViewMode.Insert; but I don't know how to programatically assign the user values to the fields, since I do not know how to get a handle to the field.

How does one retain the user input values in insert mode when canceling an inserting event in the DetailsView/ObjectDataSource programming model?

Thanks,

Paul

You should validate the input values in the DetailsView ItemInserting event, rather than in the ObjectDataSource Inserting event. In such case the input values will be retained though you cancel the inserting event

protected void DetailsView1_ItemInserting (object sender, DetailsViewInsertEventArgs e)
{
// validate
e.Cancel = true;
}

Thanks

-Mark post(s) as "Answer" that helped you

No comments:

Post a Comment