How does FormView EditTemplate update values in ObjectDataSource UpdateParameters under the hood?

Leo Nix picture Leo Nix · Apr 19, 2009 · Viewed 13k times · Source

I have a FormView bound to an ObjectDataSource.

* ObjectDataSource definition (omitted portion of it for simplicity)*

<asp:ObjectDataSource 
    ID="odsHousehold" 
    runat="server"
    TypeName="BLL.Households"
    ConflictDetection="OverwriteChanges"
    UpdateMethod="UpdateHousehold" 
    >
    <UpdateParameters>
        <asp:Parameter Name="sName" Type="String" Direction="Input" />
        <asp:Parameter Name="sAddress" Type="String" Direction="Input" DefaultValue="" />
        <asp:Parameter Name="sCity" Type="String" Direction="Input" DefaultValue="" />
        <asp:Parameter Name="sState" Type="String" Direction="Input" DefaultValue="" />
        <asp:Parameter Name="sZip" Type="String" Direction="Input" DefaultValue="" />
    </UpdateParameters>
</asp:ObjectDataSource>

* FormView definition (omitted portion of it for simplicity) *

   <asp:FormView 
    ID="fvHousehold"
    runat="server"
    DataKeyNames="HouseholdID"
    DataSourceID="odsHousehold"
    HorizontalAlign = "Left"
 >
<EditItemTemplate>
<asp:TextBox ID="txtHouseHoldName" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("HouseholdName") %>'></asp:TextBox>
<asp:TextBox ID="txtAddress" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("Address") %>'></asp:TextBox>
<asp:TextBox ID="txtCity" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("City") %>'></asp:TextBox>
<asp:TextBox ID="txtState" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("State") %>'></asp:TextBox>
<asp:TextBox ID="txtZip" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("Zip") %>'></asp:TextBox>
 <asp:Button ID="btnUpdateHousehold" runat="server" Text="Update" CommandName="Update" />
</EditItemTemplate>
</asp:FormView>

I'd like to know: how does the FormView know which UpdateParameter to populate with which EditTemplate TextBox when the Update button is clicked?
For instance, I haven't instructed "txtAddress" in the FormView to populate the UpdateParameter "sAddress" but InputParameters["sAddress"] contains the Text value of txtAddress. How does it know to do that?

Could any guru enlighten me?

Thank you so much,

Cullen

Answer

James in Indy picture James in Indy · Jul 27, 2009

"how does the FormView know which UpdateParameter to populate with which EditTemplate TextBox when the Update button is clicked?"

I believe the simple answer is: it knows because of the Bind statements you put in the TextBox controls. E.g. txtAddress has "Bind("Address")" so when the update is called, it has a connection between txtAddress and parameter "Address"