468,115 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,115 developers. It's quick & easy.

Can't access DataItem in FormView when updating

I'm using ObjectDatasource in combination with a Formview. The
ObjectDatasource is connected to a FormView.

I have a business object property which I can't update through the normal
binding since it's a custom enum type. I managed to access the buinsess
object through the FormView.DataItem property in the OnDataBinding event so
that I can fill a dropdownlist with the correct value by casting the enum
type to an int.

However,when trying to get the data OUT of the control into the business
object I can't seem to get access to the DataItem (i.e. the business object)
since it's null in the OnItemInserting and OnItemUpdating events. I tried
all kinds of things to get hold of the business object but nothing seems to
work.

Anyone know how to get the DataItem when saving?
Nov 21 '08 #1
1 6280

"Bresco" <br****@mixmaster.orgwrote in message
news:gg**********@aioe.org...
I'm using ObjectDatasource in combination with a Formview. The
ObjectDatasource is connected to a FormView.

I have a business object property which I can't update through the normal
binding since it's a custom enum type. I managed to access the buinsess
object through the FormView.DataItem property in the OnDataBinding event
so that I can fill a dropdownlist with the correct value by casting the
enum type to an int.

However,when trying to get the data OUT of the control into the business
object I can't seem to get access to the DataItem (i.e. the business
object) since it's null in the OnItemInserting and OnItemUpdating events.
I tried all kinds of things to get hold of the business object but nothing
seems to work.

Anyone know how to get the DataItem when saving?
Well, I 'solved' it, although I still find it very unintuitive that ASP.NET
does not make the DataItem available in the OnInserting, OnUpdating events.

Here's what I did:

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs
e)
{
DropDownList ddl = this.FormView1.FindControl("DropDownlist1") as
DropDownList;
e.NewValues.Add("EnumTypeProperty", ddl.SelectedIndex.ToString());
}

For some reason, ASP.NET seems to automatically cast the string in the
EnumTypeProperty to EnumType before putting it in the property.

Note that to get the property into the DropDownList you have to handle the
DropDownList1_DataBinding like:

protected void DropDownList1_DataBinding(object sender, EventArgs e)
{
MyBizObject bo = FormView1.DataItem as BizObject;
DropDownList ddl = this.FormView1.FindControl("DropDownlist1") as
DropDownList;
ddl.SelectedIndex = (int)bo.EnumTypePropererty;
}

you can't handle the FormView1_DataBinding since the DataItem will then be
NULL.
Nov 21 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by philjhanna | last post: by
4 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.