DetailsView inserting/updating with templates
I was having a problem with template fields in a details view, where it doesn't automatically get the value from a template field. The problem is the template shows up as a control through FindControl and a child control of the cell, whereas boundfields, etc. can be retrieved through the collection of values that are stored in the event argument. The initial bind is OK, but like with the insert, binding isn't provided for both directions. The problem is that the properties for those templates aren't in the collection, and aren't automatically updated, at least that is what I was having problems with. I was getting SqlExceptions for the insert statements, stating that the value was null. So how do you ensure that it is there, without custom inserting/updating?
The way that I did it, whether right or wrong, is to attach to the ItemInserting event. Then, I retrieve the value from the control, and append it to the e.Values collection, which is an IOrderedDictionary. For ItemUpdating, it is e.NewValues. So my ItemInserting would look like:
FormView view = (FormView)sender;
DropDownList box = view.FindControl("DDL") as DropDownList;
e.Values.Add("State", box.SelectedValue);
I haven't tried this with the updating yet, but I did this with insertion, and it worked great. I don't know if this is a good idea or not, but I don't see any other day.
Comments