CodeSmith Community
Your Code. Your Way. Faster!

Multiple FormViews - Single Update Button

Latest post 03-24-2008 8:32 AM by Amy. 13 replies.
  • 08-16-2006 10:30 AM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Multiple FormViews - Single Update Button

    Hi All!

    Wouldn't it be nice to have a way to have multiple FormViews in a web page (with multiple data sources, of course), and be able to submit them all with a single button (Update or Insert)? This is not a netTier specific request however. This problem is generalized in ASP.NET 2.0. But I thought that maybe the netTier people could come up with a solution to this problem.

    Cheers!

    JF

    • Post Points: 55
  • 08-17-2006 8:54 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Multiple FormViews - Single Update Button

    This can be done using the various relationship controls that are generated for the WebLibrary project.  Take a look over in the Documentation forum for some examples.

    Hope that helps.

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 08-17-2006 11:42 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Multiple FormViews - Single Update Button

    Hi Bobby!

    I looked at the examples in the documentation (mainly Many-to-Many Relationship using Web Library Controls and Using the ManyToManyViewRelationship Control) and I noticed that they all use Entity Data Sources. Can I achieve the same with Strongly Typed Data Source?

    Also, even after studying these examples, I don't see how I could get a web page with two ForViews having each its own data source and a single Update button.

    Let's say we have this schema:

    Person ( PersonId PK, FirstName, LastName, ... )
    Address ( AddressId PK, PersonId FK, AddressType, StreetAddress, City, ZIP )

    Let's say AddressType = 1 for main addresses, and that there is only one main address per person.

    A simplified example of what I would like to achieve is a form with 5 textboxes: Firstname, Lastname, Address, City, ZIP; and 2 buttons: Cancel, Update. The address information displayed on the form should be the one from the main address, just so that there is only one address to display per person.

    Ideally, I would like Firstname and Lastname to be in a FormView and Address, City, and ZIP to be in a second FormView. Of course, the Update button should update the two data sources.

    Is this something that can be achieved with the Web Library or with other tools provided with .netTiers?

    Best regards,

    JF

    • Post Points: 35
  • 08-18-2006 12:12 AM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Multiple FormViews - Single Update Button

    Yes, the relationship controls will work with all of the generated data source controls.  For the scenario you describe, you can use the OneToOneRelationship control.  For example:

    <data:OneToOneViewRelationship ID="PersonAddressRelationship" runat="server">
       <PrimaryMember runat="server"
          DataSourceID="PersonDataSource"
          EntityKeyName="PersonId" />
       <ReferenceMember runat="server"
          DataSourceID="AddressDataSource"
          ViewControlID="AddressFormView"
          EntityKeyName="AddressId" />
    </
    data:OneToOneViewRelationship>

    The first FormView will be bound to the PersonDataSource, and it will also contain your Update button.  The second FormView will be bound to the AddressDataSource.  Also, be sure to set the FormView.DataKeyNames properties.

    Hope that helps!


    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 55
  • 08-19-2006 3:26 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Multiple FormViews - Single Update Button

    Thanks Bobby!

    Yes that helps a lot! It is exactly what I needed! Do you know if there is a way to put the Update button in the Referenced FormView (AddressFormView)? I would need this in order to have a layout where the buttons are at the bottom of the page.

    Cheers!

    JF

    • Post Points: 35
  • 08-19-2006 5:18 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Multiple FormViews - Single Update Button

    Glad to hear that it met your needs!  There are several things you can do to have the buttons appear at the bottom of the page.

    1. Wrap the buttons in a DIV tag and use CSS to position the buttons below the AddressFormView.
           <div style="position:absolute; margin-top:100px;"><asp:Button /></div>

    2. Nest the AddressFormView inside of the PersonFormView.

    3. Place the <asp:Button /> tags outside of the PersonFormView and manually call the Update method.
           public void BtnUpdate_Click(object sender, EventArgs e)
           {
              PersonFormView.Update(true);
           }

    Hope that helps.

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 08-20-2006 11:13 AM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Multiple FormViews - Single Update Button

    Thanks Bobby for that quick reply! Considering that it is the weekend too...

    I tried the 3 solutions you suggested and even a fourth one, and here are the results, just so that other people looking for the same info know:

    1. That worked
    2. Did not work. As soon as I nest the referenced FormView (AddressFormView) inside the primary FormView (PersonFormView), the One-to-One View Relationship seems to get broken, and the referenced data source does not get updated. This is too bad, because I thought this was the most elegant solution.
    3. The thirsd solution worked too with PersonFormView.UpdateItem(true); instead of PersonFormView.Update(true);
    4. I also thought that adding a third FormView (Let's call it ButtonFormView) at the bottom of my page with just the buttons and ading the following code would work, but it did not:

    <data:OneToOneViewRelationship ID="PersonAddressRelationship" runat="server">
       <PrimaryMember runat="server"
          DataSourceID="PersonDataSource"
          ViewControlID="ButtonFormView"
          EntityKeyName="PersonId" />
       <ReferenceMember runat="server"
          DataSourceID="PersonDataSource"
          ViewControlID="PersonFormView"
          EntityKeyName="PersonId" />
       <ReferenceMember runat="server"
          DataSourceID="AddressDataSource"
          ViewControlID="AddressFormView"
          EntityKeyName="AddressId" />
    </data:OneToOneViewRelationship>

    Anyway, solutions 1 and 3 are good enough for me. As I mentionoed, I am posting this for future reference.

    Cheers, and many thankd for all your help!

    JF


    • Post Points: 35
  • 08-20-2006 1:17 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Multiple FormViews - Single Update Button

    Sorry about #2.  I forgot to mention that both the PersonAddressRelationship control and the AddressFormView control would need to be nested inside of the PersonFormView in order for them to be bound correctly.

    And you are correct about #3.  That is what happens when I try to code from memory!

    As for #4, I understand what you were trying to do, but I can't think of a way of getting it to work since that was not how they were intended to be used...

    Thanks for posting your results!

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 08-24-2006 10:25 AM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Multiple FormViews - Single Update Button - nested One-to-One relationships

    Hi!

    I tried adding a thrid level of FormView on the web page, and fetching the data works, but not updating it. Here is an example what I am using. For the sake of this example, let's suppose I now have a third table just for the Postal Codes: PostalCodes ( PostalCodeId PK, Code, ... )

    <data:OneToOneViewRelationship ID="PersonAddressRelationship" runat="server">

       <PrimaryMember runat="server"
          DataSourceID="PersonDataSource"
          EntityKeyName="PersonId" />
       <ReferenceMember runat="server"
          DataSourceID="AddressDataSource"
          ViewControlID="AddressFormView"
          EntityKeyName="AddressId" />
    </
    data:OneToOneViewRelationship>

    <data:OneToOneViewRelationship ID="AddressPostalCodeRelationship" runat="server">
       <PrimaryMember runat="server"
          DataSourceID="AddressDataSource"
          ViewControlID="AddressFormView"
          EntityKeyName="AddressId" />
       <ReferenceMember runat="server"
          DataSourceID="PostalCodeDataSource"
          ViewControlID="PostalCodeFormView"
          EntityKeyName="PostalCodeId" />
    </
    data:OneToOneViewRelationship>

    Is this supposed to work? Or am I going the wrong path? In other words, is it possible to have nested One-to-One relationships?

    Cheers!

    JF

    • Post Points: 35
  • 08-24-2006 11:15 AM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Multiple FormViews - Single Update Button - nested One-to-One relationships

    Yes, you should be able to chain relationships together like you are trying to do, so you might have something set up incorrectly.  Let me see if I get what you are trying to do.  With the example you posted, it seems as though you want to update a record in your PostalCode table every time you update an Address record.  Is that correct?

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 60
  • 08-24-2006 4:13 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Multiple FormViews - Single Update Button - nested One-to-One relationships

    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

     

    • Post Points: 5
  • 08-30-2006 11:42 AM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Multiple FormViews - Single Update Button - nested One-to-One relationships

    Hi Bobby!

    Yes, that's correct, I want to update a record in my PostalCode table every time I update an Address record, but not only that, tehre are three levels here Person-Address-PostalCode. So, to be more precise,, everytime that I update a person's record, I want to update the person's address and PostalCode.

    I know my previous post was a bit long, and it migth be scary to see long listings in post when the says are too short for everything there is to do, but I tought it would be a good idea for other new users to be able to see a complete example.

    Cheers!

    JF

    • Post Points: 5
  • 09-03-2006 5:17 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: Multiple FormViews - Single Update Button - nested One-to-One relationships

    Ok, I've tried my code above (See Post 16944), and made a few corrections to it to make it work. I hope I did not loose anything while formatting the source code. So it works. Now, I should review the code of my real project to make it work too.

     

    • Post Points: 35
  • 03-24-2008 8:32 AM In reply to

    • Amy
    • Top 200 Contributor
    • Joined on 05-04-2006
    • Posts 21
    • Points 465

    Re: Multiple FormViews - Single Update Button - nested One-to-One relationships

    Hi velum ,

    I am trying to do the same thing, but I get some questions. If a person already has address info in the database, the one-to-one relation works beautifully.  If there is person info without address, how can you make the person formview in edit mode and address formview in insert mode?  Do you use it for insert person and address?  I tried it,  it seems the inside adreessformview always got inserting 1st that will fail since there is no PersonId yet. Thanks!Amy

     

    • Post Points: 5
Page 1 of 1 (14 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems