What I am trying to do is to be able to updates fields on three levels, eg. the person name, the address, and the ZIP. I know it does not make much sense in real life to put the postal code in a third table, but this is just for the sake of my example. Bellow is a simplified version of what I am trying to do. I just wrote this to give an idea of what I am trying to do. I also made a more complete example, but without any formatting code.
Suppose we have this schema:
Person (PersonId PK, FirstName, LastName, ...)
Address ( AddressId PK, PersonId FK, AddressType,
StreetAddress, CityID FK, StateID FK, PostalCodeID FK)
PostalCodes ( PostalCodeId PK, Code, ... )
Cities (CityID PK, CityName)
States (StateID PK, StateName)
And the following aspx page below. I know the code is a bit long, but it might be useful for people trying to do something similar in the future (I would have liked to highlight some parts but this feature does not work in this forum):
<%@ Page ="C#" ="true" ="WebForm1.aspx.cs" ="Project.Website.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Nested One-to-One relationships example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="PersonFormView" runat="server" DefaultMode="Edit"
DataSourceID="PersonDataSource" DataKeyNames="PersonID">
<EditItemTemplate>
<asp:TextBox ID="PersonFirstName" runat="server" Text='<%# Bind("FirstName") %>' />
<asp:FormView ID="AddressFormView" runat="server" DefaultMode="Edit"
DataSourceID="AddressDataSource" DataKeyNames="AddressID">
<EditItemTemplate>
<asp:TextBox ID="PersonAdresse" runat="server" Text='<%# Bind("StreetAddress") %>' />
<asp:DropDownList ID="CityDropDownList" DataSourceID="CitiesSourceID"
DataValueField="CityID" DataTextField="CityName"
SelectedValue='<%# Bind("CityID") %>' runat="server">
</asp:DropDownList>
<asp:DropDownList ID="StateDropDownList" DataSourceID="StatesSourceID"
DataValueField="StateID" DataTextField="StateName"
SelectedValue='<%# Bind("StateID") %>' runat="server">
</asp:DropDownList>
<asp:Label ID="PostalCodeIDLabel" runat="server" Text='<%# Bind("PostalCodeID ") %>' Visible="false" />
<asp:FormView ID="PostalCodeFormView" runat="server" DefaultMode="Edit"
DataSourceID="PostalCodeDataSource" DataKeyNames="PostalCodeID">
<EditItemTemplate>
<asp:TextBox ID="PostalCode" runat="server" Text='<%# Bind("Code") %>' />
</EditItemTemplate>
</asp:FormView>
<data:OneToOneViewRelationship ID="AddressPostalCodeRelationship" runat="server">
<PrimaryMember ID="PrimaryMember" runat="server"
DataSourceID="AddressDataSource"
ViewControlID="AddressFormView"
EntityKeyName="AddressID" />
<ReferenceMember ID="ReferenceMember" runat="server"
DataSourceID="PostalCodeDataSource"
ViewControlID="PostalCodeFormView"
EntityKeyName="PostalCodeID" />
</data:OneToOneViewRelationship>
<data:PostalCodeDataSource ID="PostalCodeDataSource" runat="server"
EnableDeepLoad="false" SelectMethod="GetByPostalCodeID" >
<Parameters>
<asp:ControlParameter name="PostalCodeID" ControlID="PostalCodeIDlabel" />
</Parameters>
</data:PostalCodeDataSource>
</EditItemTemplate>
</asp:FormView>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<data:OneToOneViewRelationship ID="PersonAddressRelationship" runat="server">
<PrimaryMember ID="PrimaryMember" runat="server"
DataSourceID="PersonDataSource"
EntityKeyName="PersonId" />
<ReferenceMember ID="ReferenceMember" runat="server"
DataSourceID="AddressDataSource"
ViewControlID="AddressFormView"
EntityKeyName="AddressID" />
</data:OneToOneViewRelationship>
</FooterTemplate>
</asp:FormView>
<data:PersonDataSource ID="PersonDataSource" runat="server"
EnableDeepLoad="false" SelectMethod="GetByPersonID">
<Parameters>
<asp:QueryStringParameter Name="PersonID" QueryStringField="id" Type="String" />
</Parameters>
</data:PersonDataSource>
<data:AddressDataSource ID="AddressDataSource" runat="server"
EnableDeepLoad="true" SelectMethod="GetByPersonID">
<DeepLoadProperties Method="IncludeChildren" Recursive="true">
<Types>
<data:AddressProperty Name="Cities" />
<data:AddressProperty Name="States" />
</Types>
</DeepLoadProperties>
<Parameters>
<asp:QueryStringParameter Name="PersonID" QueryStringField="id" Type="String" />
<data:CustomParameter
Name="WhereClause"
Value="AddressType=1"
ConvertEmptyStringToNull="true"
/> <%-- AddressTypeID=1 ==> Main Address --%>
</Parameters>
</data:AddressDataSource>
<data:CitiesDataSource ID="CitiesDataSource" SelectMethod="GetAll" runat="server" />
<data:StatesDataSource ID="StatesDataSource" SelectMethod="GetAll" runat="server" />
</div>
</form>
</body>
</html>
When I tried something similar, I was able to see the data, but I was not able to update the data in the third FormView, ie the postal code.
Cheers!
JF