FormView w/ CslaDataSource not updating when BO properties are changed!?

FormView w/ CslaDataSource not updating when BO properties are changed!?

Old forum URL: forums.lhotka.net/forums/t/2111.aspx


SonOfPirate posted on Wednesday, January 10, 2007

I have a FormView control bound to a CslaDataSource representing a Project in the application.  Part of the business logic calls for us to "cascade" some property values when a Customer is selected from a DropDownList in the form.  The DropDownList is also bound using the CslaDataSource control and the OnSelectedIndexChanged handler is defined declaratively as follows:

<asp:DropDownList ID="CustomerDropDownList" runat="Server" AutoPostBack="true" DataSourceID="CustomersDataSource" DataTextField="Name" DataValueField="UniqueID" OnSelectedIndexChanged="CustomerDropDownList_SelectedIndexChanged" Text='<%# Bind("Customer") %>' />

In the code-beside, I have:

protected void CustomerDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    Customer c = Customer.GetCustomer(new Guid(((DropDownList)sender).SelectedValue));
    Project p = GetProject();

    // copy some properties from "c" to "p"

    FormView1.DataBind();
}

First, I get the Customer object based on the selected item in the drop down list.  Then I get the Project that is the data source for the form and copy some property values from the Customer to the Project.  Finally, I rebind the FormView control (cuz this was all I could find to do in other forums to try and fix my problem).

When I put breakpoints in my code and step through, the CslaDataSource's SelectObject handler does fire after the DataBind statement and the updated BO is set to the e.BusinessObject property.  However, the FormView's field do not reflect the changes made in the SelectedIndexChanged handler - they come up blank as they did originally.

What am I doing wrong?  How is it possible for the e.BusinessObject to reflect the changes yet the page renders unchanged?

 

skagen00 replied on Wednesday, January 10, 2007

I'm not sure if this is what is coming into play but I thought I'd shoot a quick reply.

If you're rebinding the dropdownlist to the datasource on every post back, you may lose your selected index for your event. (At least there are instances where this sort of thing has come into play for me).

SonOfPirate replied on Wednesday, January 10, 2007

I think you may be on to something.  I didn't realize that the DropDownList is also rebound when the FormView1.DataBind method is called.  I put in a breakpoint and confirmed that this is happening.

What confuses me is that the DropDownList is bound to the BO's Customer property which is set to the selected item in the SelectedIndexChanged handler - before the DataBind method call.  So, even though we re-retrieve the CustomerList to bind the DropDownList to, when the SelectedItem property is set, shouldn't it be set to the BO's Customer property which, as I said, was reset to the previously selected item?

And, why does this affect the other fields in the FormView which are bound to other properties of our BO?

Finally, what is the fix???

 

skagen00 replied on Thursday, January 11, 2007

I use individual controls rather than FormsView, etc - and essentially I bind dropdownlists to the datasource when !IsPostBack only.

I'm not entirely sure what the answer is for you, I haven't used FormsView or any of those ilk for a while for edit controls.

Copyright (c) Marimer LLC