overriding how a particular DB column is displayed

Last post 12-12-2008 12:27 PM by gstadter. 14 replies.

Sort Posts:

  • overriding how a particular DB column is displayed

    11-19-2008, 12:36 PM
    • Member
      45 point Member
    • gstadter
    • Member since 01-18-2007, 6:14 PM
    • Posts 132

    I am integrating DynamicData into an existing website, to take advantage of, among other things, DynamicFields.
    My quandry is this:
    When doing an "insert" of a new record into a "Coordinators" table, one of the columns is "city_ID".   This "city_ID" has a foreign key relationship to the "city_ID" column of the "Cities" table.
    Of course, there is a "City" column in the "Cities" table, which holds the 'common' name of the city.

    What I would like, is that when the "Coordinators" table record is being viewed, that the 'common name' be shown, as well as the dropdown, in insert or edit mode, show all of the values in the "City" column to the user, but insert the value for 'city_ID' on edit/insert.

    What is the best way to accomplish this, using DynamicData?

    Is it something that can be done globally(in the class), or is this manipulation within the code of each page?

    Thank you for any ideas or suggestions!

    ,Gary 

     

  • Re: overriding how a particular DB column is displayed

    11-19-2008, 2:41 PM
    Answer
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: overriding how a particular DB column is displayed

    11-20-2008, 10:26 AM
    • Member
      45 point Member
    • gstadter
    • Member since 01-18-2007, 6:14 PM
    • Posts 132

    Precisely what I needed, Thank you!

  • Re: overriding how a particular DB column is displayed

    11-25-2008, 3:35 PM
    • Member
      45 point Member
    • gstadter
    • Member since 01-18-2007, 6:14 PM
    • Posts 132

    Please forgive me for asking a novice question, but...
    Is there no way to just "tap into" the existing templates(ForeignKey_Edit, I think), as opposed to creating custom ones?
    When I do an "Insert" via the auto-scaffolded pages(page tamplates), the fields that are foreign keys are represented as dropdowns and the dropdowns are populated with "[not set]" in addition to the corrosoponding values in the foregn table.
    I may be showing how much I have yet to learn about how DynamicData works, but even though there may not be another option, it "feels" like I am re-creating the wheel by creating custom tempates.

    Thank you,
    Gary

  • Re: overriding how a particular DB column is displayed

    11-25-2008, 4:17 PM
    • Contributor
      5,874 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 941
    • AspNetTeam
      Moderator

    Hey Gary,

    What exactly do you want to do with the existing templates?  For page templates, see How to: Customize the Layout of an Individual Table By Using a Custom Page Template
    For field templates (like ForeignKey_Edit), you can modify the template but it impacts the entire scaffold (every entity that uses the field template).

    ASP.NET Dynamic Data Default Field Templates -
    How to: Customize Data Field Display in the Data Model  - shows how to create a custom field template.

    L

    Rick -ASP.Net UE MVC FAQ   Rick on MVC & Dynamic Data   
  • Re: overriding how a particular DB column is displayed

    11-26-2008, 9:06 AM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571

    Hi Gary, I think I've got what you want to do now for the post that appeared 3 times then disappeared you want the state name to appear when in List and Details but you want the FK field to show in Edit and Insert. Am i right?

    If so then have a look at this article here:

    Dynamic Data - Hiding Columns in selected PageTemplates

    You need this because you are dealing with two different fields

    1. The StateID the FK field
    2. State the EntitySet field

    So in your metadata you would show the FK field using an ScaffoldColumn(true) attribute and then use the new attribute defined in the example to hide and show field in the appropriate Views.

    Hope this make sense Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: overriding how a particular DB column is displayed

    11-26-2008, 11:47 AM
    • Member
      45 point Member
    • gstadter
    • Member since 01-18-2007, 6:14 PM
    • Posts 132

    I do not want to modify the way the templates display the fields.
    What I was trying to figure out is how to get an integer field of a record to display in a detailsview on a page, not using the dynamicdata page templates, with the same ForeignKey_Edit template that the auto-scaffoded page is obviously using.

    I changed the following within <Fields> of my detailsview:

    <asp:DynamicField DataField=”state_ID” />
    to
    <asp:DynamicField DataField=”States” />

     

    Now, I get the dropdown list that I’ve been attempting to reproduce, but am unsure of how to get the corrosponding integer value to be recorded upon insert.

    Any suggestions on how to accomplish that part of it?  Can it be done within the control, or might some code be required?

    Thank you,
    Gary

  • Re: overriding how a particular DB column is displayed

    12-02-2008, 9:32 AM
    • Member
      45 point Member
    • gstadter
    • Member since 01-18-2007, 6:14 PM
    • Posts 132

    Steve,

    to more specifically answer your question at the top of your most recent contribution to this thread:
    I'm not wanting to manipulate the DynamicData page templates.  In fact, I'm not using the page templates at all.
    All I am wanting to do is take advantage of the field templates within a detailsview in insert mode on a page outside of the DynamicData folder.

    So, at the risk of sounding redundat, where I am at now is needing to figure out how to get the selected value fom the "States" (EntityRef) to get inserted via my insert mode detailsview, as it does within the Insert page template.

    Thank you for any suggestions,

    Gary

  • Re: overriding how a particular DB column is displayed

    12-03-2008, 5:14 AM
    Answer
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571

    The first thing to understand is that in your case the differance between state_ID and State is on is the actual column FK and the other reresents the relationship which is standard Linq to SQL and EF, DD treats the column and the Entity Ref differently first of all the FK fields are not displayed by default ScaffoldColumn(false) is implied. If you want the FK field to display in standard PageTemplates you will need to add ScaffoldColumnAttributes to your metadata.

    [MetadataType(typeof(Order_DetailMD))]
    public partial class Order_Detail
    {
        public class Order_DetailMD
        {
            public object OrderID { get; set; }
            [ScaffoldColumn(true)]
            public object ProductID { get; set; }
            public object UnitPrice { get; set; }
            public object Quantity { get; set; }
            public object Discount { get; set; }
            public object Order { get; set; }
            public object Product { get; set; }
        }
    }
    Does that make some sense?
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: overriding how a particular DB column is displayed

    12-03-2008, 5:33 AM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571

    My post seems to have gone Sad

    And try again...

    The differance between the FK field and the EntitySet fields are what you need to undestand in you case state_ID is an FK field and State is an EntitySet so when you see a drop down list it is the rendering of the EntitySet and the EntitySet represent the relationship between this table and the one on the other end of the FK. So when you make a change to the DropDownList (EntitySet) the change will be reflected in the FK field automatically, this is because the EntitySet is just a representation of the FK relationship.

    Hope this make sense. Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: overriding how a particular DB column is displayed

    12-03-2008, 5:35 AM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571

    oops now its back just pick which one you want to read Stick out tongue

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: overriding how a particular DB column is displayed

    12-04-2008, 3:17 PM
    • Member
      45 point Member
    • gstadter
    • Member since 01-18-2007, 6:14 PM
    • Posts 132

    Is it not possible to reproduce EntitySet functionality outside of the PageTemplates?
    I am esentailly integrating DD into an existing website, so I am utilizing DynamicFields(fieldTemplates) inside of GridViews and DetailViews, rather than using the PageTemplates.
    I want the site to function with the PageTemplates folder deleted from within DynamicData.

    It feels as though I'm 98% there because I can place a DynamicField in a DetailsView, point it at the EntitySet "States" and get the same dropdown list that I would within the "Insert.aspx" PageTemplate.

    What I haven't figured out is how to bind the selected value upon insert, as the "Insert.aspx" Page Template appears to be able to do.

    As is, I get no 'error'.   Just no value gets passed(written to new record in db).

    ,Gary

    Am I "breaking ground" here, attempting to inetgrate DD into an existing site, not utilizing the PageTemplates, with a db that utilizes FKs?
    surely not.

     

     

  • Re: overriding how a particular DB column is displayed

    12-04-2008, 6:31 PM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571

    Hi Gary, no your not I have some article on custom pages here:

    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

    Which may be worth taking a look at.

    The main thing to realise with DD is that you've got to work with entities so you can't just bring back the columns you want with a linq query you must bring back the entity as represented in the model.

    Hope that makes sense.

    Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: overriding how a particular DB column is displayed

    12-10-2008, 2:58 AM
    • Participant
      1,320 point Participant
    • scothu
    • Member since 12-10-2007, 6:54 AM
    • Redmond, WA
    • Posts 272
    • Moderator

    If you want to see an example of using Dynamic Data in standalone pages you might want to checkout this page: http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14473. Both the ExistingWebsite and ExistingWebsite_Entities show how to use it in regular pages including foreign key columns. The main thing you need to make sure you are doing is adding a DynamicDataManager on your page and registering you data control (GridView, DetailsView, FormView and ListView) with it. The page templates have no magic in them all the source code is there for you to see.

    Scott Hunter
    PM, ASP.NET Team, Microsoft
  • Re: overriding how a particular DB column is displayed

    12-12-2008, 12:27 PM
    • Member
      45 point Member
    • gstadter
    • Member since 01-18-2007, 6:14 PM
    • Posts 132

    I've begun to look through the sample sites.

    My page already had a DynamicDataManager control, but I hadn't regidtered my DetailsView.

     

    I do though get the following intellisense error( on "DynamicDataManager1"), prior to compilation:

     

     "Cannot access a non-static member of outer type 'ASP.gettingstarted_coordinatornew_aspx' via nested type 'ASP.gettingstarted_coordinatornew_aspx.CoordinatorNew'"

     

    here is the top portion of my page:

    <%@ Page EnableViewState="false" Language="C#" AutoEventWireup="true" MasterPageFile="gs.Master" %>
    <%
    @ Import Namespace="System" %>
    <%
    @ Import Namespace="System.Data" %>
    <%
    @ Import Namespace="System.Configuration" %>
    <%
    @ Import Namespace="System.Collections" %>
    <%
    @ Import Namespace="System.Linq" %>
    <%
    @ Import Namespace="System.Web" %>
    <%
    @ Import Namespace="System.Web.Security" %>
    <%
    @ Import Namespace="System.Web.UI" %>
    <%
    @ Import Namespace="System.Web.UI.WebControls" %>
    <%
    @ Import Namespace="System.Web.UI.WebControls.WebParts" %>
    <%
    @ Import Namespace="System.Web.UI.HtmlControls" %>
    <%
    @ Import Namespace="System.Xml.Linq" %>
    <%
    @ Import Namespace="System.Web.DynamicData" %>

    <script runat="server" language="C#">

    void Page_Load()

    {

    int userID = CommunityServer.Components.CSContext.Current.User.UserID;string CSUserName = CommunityServer.Components.CSContext.Current.User.Username;

    Label1.Text = CSUserName;

    if (!user.IsAnonymous)

    {

    string titleText = string.Format(CommunityServer.Components.ResourceManager.GetString("ViewUserProfile_UserFriendsActivity"), user.DisplayName);

    string aggRssUrl = CommunityServer.Components.SiteUrls.Instance().UserFriendsActivityRss(user.Username);

    }

    }

    public partial class CoordinatorNew : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e) {

    DynamicDataManager1.RegisterControl(DetailsView1);

    }

    }

Page 1 of 1 (15 items)