3

MS DynamicData's Children.ascx.cs file has a Page_Load method that returns a hyperlink which says "View Children". I want to append the number of children to the end of the hyperlink text. Below is my attempt. How can I make the hyperlink say "View Children - # entries" ?

protected void Page_Load(object sender, EventArgs e)
{
    HyperLink1.Text = "View " + ChildrenColumn.ChildTable.DisplayName;

    //The following code gives the total entries.
    //How do I get the number of children only?
    //int entries = 0;
    //foreach (var entry in ChildrenColumn.ChildTable.GetQuery()) { entries++; }
    //string entryText = (entries == 1) ? "entry" : "entries";
    //HyperLink1.Text= HyperLink1.Text + " " + entries + " " + entryText;
}
Benjamin
  • 3,134
  • 6
  • 36
  • 57

4 Answers4

3

Actually it is not that hard. You can add the following Method to your Children.ascx.cs file:

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);

        object entity;
        ICustomTypeDescriptor rowDescriptor = Row as ICustomTypeDescriptor;
        if (rowDescriptor != null)
        {
            // Get the real entity from the wrapper
            entity = rowDescriptor.GetPropertyOwner(null);
        }
        else
        {
            entity = Row;
        }

        // Get the collection and make sure it's loaded
        RelatedEnd entityCollection = Column.EntityTypeProperty.GetValue(entity, null) as RelatedEnd;
        if (entityCollection == null)
        {
            throw new InvalidOperationException(String.Format("The Children template does not support the collection type of the '{0}' column on the '{1}' table.", Column.Name, Table.Name));
        }
        if (!entityCollection.IsLoaded)
        {
            entityCollection.Load();
        }

        int count = 0;
        var enumerator = entityCollection.GetEnumerator();
        while (enumerator.MoveNext())
            count++;

        HyperLink1.Text += " (" + count + ")";
    }
frast
  • 2,700
  • 1
  • 25
  • 34
  • Thank you for your reply. I probably will not get around to testing this as I abandoned DD in favor of MvcScaffolding. – Benjamin Jan 26 '12 at 14:12
  • I would be interested in how you would do this in MvcScaffolding. Because I think about using it too. – frast Jan 26 '12 at 14:21
  • did you mean how to display the count of a collection? Or how to use MvcScaffolding in general? MvcScaffolding actually displays the count of a collection navigation property by default. I'm using EntityFramework.Migrations 0.8.0.0 from NuGet which also installs Entity Framework 4.2, I think. I'm designing the model "code first" and then running MvcScaffolding for the controllers and views in the Package Manager console using `foreach`. It still needs customization in the T4 scaffolding tempates, which I am about to start learning. – Benjamin Jan 26 '12 at 18:22
1

well, HyperLink1.Text ="SomeString" should make your hyperlink's text be "SomeString"

HyperLink1.Text = "View Children -"+numEntries+" entries";

should make the hyperlink say what you want it to say, so long as numEntries is the right number at the time, at least it works that way on my machine ..

What is the current result of your attempt?

  • I want the number of children. My code above gives the count of the entire table that the children are stored in. I don't know how to get the number of children. I was learning Entity Framework & MVC before but now I've added this weird web forms stuff. I do know how to concatenate a string. But I don't know how to get the number of children here in this DD template. – Benjamin Jan 16 '12 at 18:37
  • Where is the table declared? What type is it? – Sam I am says Reinstate Monica Jan 16 '12 at 18:55
  • I don't know. In Global.asax there's `MetaModel.RegisterContext(...)` ? It's DynamicData scaffolding - not my own personal code. The type of the table is `MetaTable`. The particular model type must be assigned runtime or else how could it be dynamic right? – Benjamin Jan 16 '12 at 19:07
  • Well, how did you make the table? was the table automatically included with your project? if so, what kind of project did you create – Sam I am says Reinstate Monica Jan 16 '12 at 19:36
  • I'm using EF 4.2 CodeFirst with EF.Migrations 0.8.0.0 - the project template was an MVC 3 intranet app. Then I added DD to the MVC project. I'm not sure what you mean by "the table" - the SQLCE table? The EF DbSet? The DD MetaTable? It is confusing :) – Benjamin Jan 16 '12 at 19:41
  • First off, I don't know EF, so this will be my last comment. In a lot of .net applications, you're given a collection of some sort that represents your data. These collections often have a .Count or .Length property. They might have something along the lines of a .Entries.Count, which is probably the number you're looking for. Use Intellisence, the visual studio feature that autocompletes symbols for you. – Sam I am says Reinstate Monica Jan 16 '12 at 20:11
1

I have found a potential solution here 'FieldTemplates: Children.ascx: Displaying Count' : http://forums.asp.net/t/1466373.aspx/1

Brissles
  • 3,833
  • 23
  • 31
  • That was a good find but unfortunately all that `Expression` stuff is way over my head right now. The generic code in that link does not seem to be correctly identifying the FK column on the child table. – Benjamin Jan 17 '12 at 20:31
0

I have a very simple generic solution using dynamic:

Override the OnDataBiding method in the Childrex.aspx.cs and use the following code to get the number of child entities.

// get the field using dynamic
dynamic dynamicField = FieldValue;

// get the count property (this is a valid property for an EnitySet)
int count = dynamicField.Count;