Two-way data binding of controls in a user control inside a FormView

Posted by Sandor Drieënhuizen on Stack Overflow See other posts from Stack Overflow or by Sandor Drieënhuizen
Published on 2010-06-15T12:29:42Z Indexed on 2010/06/15 12:32 UTC
Read the original article Hit count: 396

Filed under:
|
|

I'm trying to perform two-way data binding on the controls in my user control, which is hosted inside a FormView template.

FormView:

<asp:ObjectDataSource runat="server" ID="ObjectDataSource" 
    TypeName="WebApplication1.Data" SelectMethod="GetItem" UpdateMethod="UpdateItem">
</asp:ObjectDataSource>
<asp:FormView runat="server" ID="FormView">
    <ItemTemplate>
        <uc:WebUserControl1 runat="server"></uc:WebUserControl1>
    </ItemTemplate>
    <EditItemTemplate>
        <uc:WebUserControl1 runat="server"></uc:WebUserControl1>
    </EditItemTemplate>
</asp:FormView>

User control:

<%@ Control Language="C#" ... %>
<asp:TextBox runat="server" ID="TitleTextBox" Text='<%# Bind("Title") %>'>
</asp:TextBox>

The binding works fine when the FormView is in View mode but when I switch to Edit mode, upon calling UpdateItem on the FormView, the bindings are lost. I know this because the FormView tries to call an update method on the ObjectDataSource that does not have an argument called 'Title'.

I tried to solve this by implementing IBindableTemplate to load the controls that are inside my user control, directly into the templates (just like I had entered them declaratively like in the code above). However, when calling UpdateItem in edit mode, the container that gets passed into the ExtractValues method of the template, does not contain the TextBox anymore. It did in view mode!

I have found some questions on SO that relate to this problem but they are rather dated and don't provide straight forward answers.

How do you think I could solve this problem? It seems to be such a simple requirement but apparently it's more like opening a can of worms...

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about databinding