locked
confused on getting dropdownlist working on foreign key RRS feed

  • Question

  • User300685930 posted

    I'm working with the standard (vs2008sp1) dynamic data project.  I've created a datacontext that hast two tables.  Company and CompanyType.  Company key has a foreign key to companytype and when I run my scalffolding, it shows me a dropdownlist for the companytype as I would expect. 

    When I create my own simple page and use GridView, DynamicDataManager and the Dynamic Controls, I can't seem to get the dropdown.  Just the actually companytypeId.  I'm new to this and am probably missing something simple.  I've been trying to follow the msdn example at: http://msdn.microsoft.com/en-us/library/cc488554.aspx

    Here is my code.

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    protected void Page_Init(object sender, EventArgs e)

    {

    DynamicDataManager1.RegisterControl(GridView1);

    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server"

    AutoLoadForeignKeys="true" />

     

    <asp:LinqDataSource ID="LinqDataSource1" runat="server"

    ContextTypeName="DataClassesDataContext" Select="new (id, CompanyTypeId, Name)"

    TableName="Companies">

    </asp:LinqDataSource>

     

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

    DataSourceID="LinqDataSource1">

    <Columns>

    <asp:DynamicField DataField="id" HeaderText="id" />

    <asp:DynamicField DataField="CompanyTypeId" HeaderText="CompanyTypeId" />

    <asp:DynamicField DataField="Name" HeaderText="Name" />

    </Columns>

    </asp:GridView>

    </div>

    </form>

    </body>

    </html>

    Saturday, October 4, 2008 2:25 PM

All replies

  • User660823006 posted

    Looking at your source code you are doing a projection  "new (id, CompanyTypeId, Name)"  which is why you are not getting the hookup for free. What enables the auto hookup for Dynamic Data is the fact that the classes generated by the Linq to SQL designer contain a reference to the relationship. In the case of making a custom projection like you have done an anonymous class is generated on the fly by Linq and it does not contain these relationships. Note you can create your own objects with the Linq to SQL designer that match what your projection is and you will then get the correct behavior. Let me know if you need more help with this.

    Saturday, October 4, 2008 6:34 PM
  • User-330204900 posted

    Have alook at my series on Custom pages here:

    Part 1 - Standard Custom Page Dynamic Data and Custom Pages

    As far as I can see there are three (oops! four then) types of Custom Page:

    1. Custom Pages Part 1 - Standard Custom Page based on an existing PageTemplate and customised in the DynamicData\CustomPages folder.
    2. Custom Pages Part 2 - A completely Custom Page again in the DynamicData\CustomPages folder.
    3. Custom Pages Part 3 - Standard ASP.Net Page with Dynamic Data features added to take advantage of the FieldTemplates.
    4. Custom Pages Part 4 - A DetailsView and a GridView using Validation Groups
    5. Custom Pages Part 5 - I18N? Internationalisation Custom Page  
    Saturday, October 4, 2008 6:58 PM
  • User300685930 posted

    Hi Scott,

    I'm not quite sure what LinqDataSource is doing for me.  I knew DynamicData was using the relationships in Linq to Sql so I thought the best thing to do was use the LinqDataSource.  I got rid of the new(id,..) which appears valid. Now I have the following and it is still not working.  What is the best datasource that will be associated with a presentation control?  Is LinqDataSource a good thing to use?  What I've been doing with Linq to Sql is creating my own business classes associated with ObjectDataSource and using Linq inside those classes.  Then, I've been returning generic collections, but I'm thinking the relationship to the foreign keys will certainly not come through that way.

    Thanks for the help, -Peter  (BTW, I really like the Dynamic Data design and implementation.  It just makes so much sense.  Thanks for all your work on this)

    <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DBAccess.DataClassesGeneralDataContext"

    OrderBy="Name" EnableDelete="True" EnableInsert="True" EnableUpdate="True"

    TableName="Companies">

    </asp:LinqDataSource>

    Saturday, October 4, 2008 11:58 PM
  • User-330204900 posted

    The only data souces that wotk with DD are EntityDataSource and LinqDataSource. [:D]

    Sunday, October 5, 2008 5:10 AM
  • User-330204900 posted

    <asp:DynamicField DataField="CompanyTypeId" HeaderText="CompanyTypeId" />

    Hi pkellner, I suspect you are making the same mistake I first made with DD in that asuming the FK CompanyType field on my tabel is the one that shows the drop down list when its actually the entity ref column in your case probably CompanyType, in your designer.cs file for your model look at the class properties to find the name of the entity refs.

    Hope this helps [:D]

    Sunday, October 5, 2008 5:17 AM
  • User300685930 posted

    I suspect you are making the same mistake I first made with DD in that asuming the FK CompanyType field on my tabel is the one that shows the drop down list when its actually the entity ref column in your case probably CompanyType

    Yes!  you were right.  However, not that I have fixed that, when I change the value, I get the error:

    Could not find a row that matches the given keys in the original values stored in ViewState.  Ensure that the 'keys' dictionary contains unique key values that correspond to a row returned from the previous Select operation.

     

    Sunday, October 5, 2008 11:58 AM
  • User300685930 posted

    actually, I answered my own question by reading the error message more closely (and adding DataKeyNames="CompanyTypeId,Id" To the GridView Def.

    <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DBAccess.DataClassesGeneralDataContext"

    OrderBy="Name" EnableDelete="True" EnableInsert="True" EnableUpdate="True"

    TableName="Companies">

    </asp:LinqDataSource>

     

     

     

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

    AutoGenerateColumns="false" DataSourceID="LinqDataSource1" DataKeyNames="CompanyTypeId,Id" >

    <Columns>

    <asp:CommandField ShowEditButton="true" />

    <asp:DynamicField DataField="CompanyType" />

    </Columns>

    </asp:GridView>

    Sunday, October 5, 2008 12:04 PM