locked
How can I show the child count in the Children.ascx.cs FieldTemplate? RRS feed

  • Question

  • User-1882961111 posted

    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;
    }
    Monday, January 16, 2012 3:27 PM

Answers

All replies

  • User-330204900 posted

    You will need to write some Linq Expression to evaluate it generically.

    Tuesday, January 17, 2012 12:40 PM
  • User-1882961111 posted

    I tried the following from another thread,

     protected override void OnDataBinding(EventArgs e) {
                base.OnDataBinding(e);
    
                var foreignKeyColumn = ChildrenColumn.ColumnInOtherTable as System.Web.DynamicData.MetaForeignKeyColumn;
                var query = ChildrenColumn.ChildTable.GetQuery();
                var parameter = Expression.Parameter(query.ElementType, "row");
    
                Type foreignKeyType = ChildrenColumn.ChildTable.Columns.Where(
                    c => c.Name == foreignKeyColumn.ForeignKeyNames[0]
                ).Single().EntityTypeProperty.PropertyType;
    
                int count = (int)query.Provider.Execute(
                     Expression.Call( 
                        typeof(Queryable), "Count",
                        new Type[] { query.ElementType },
                        Expression.Call( 
                            typeof(Queryable), "Where",
                            new Type[] { query.ElementType },
                            query.Expression,
                            Expression.Lambda(
                                Expression.Equal(
                                    Expression.Property(
                                        parameter, 
                                        foreignKeyColumn.ForeignKeyNames[0]
                                    ),
                                    Expression.Constant(
                                        GetColumnValue(Table.PrimaryKeyColumns[0]),
                                        foreignKeyType
                                    )
                                ),
                                parameter
                            )
                        )
                    )
                );
    
                HyperLink1.Text = count.ToString() + " " + ChildrenColumn.ChildTable.GetDisplayName();
            }

    But I get an error that no column name matches the foreign key name. Does it require a strict naming convention in the data model? This is way over my head at this point. I tried foreignkeyname.contains(c.name) or .startswith(c.name) but I got more errors. I will try again an post them unless you can show me an easier way. If I could just get the Id of the current Row then maybe I could just look it up with the DbContext which I am more familiar with.

    Tuesday, January 17, 2012 3:18 PM
  • User-1882961111 posted

    Ok, I changed 

    Type foreignKeyType = ChildrenColumn.ChildTable.Columns.Where(
                    c => c.Name == foreignKeyColumn.ForeignKeyNames[0]
                ).Single().EntityTypeProperty.PropertyType;

    to

    Type foreignKeyType = ChildrenColumn.ChildTable.Columns.Where(
    c => foreignKeyColumn.ForeignKeyNames[0].StartsWith(c.Name)
    ).Single().EntityTypeProperty.PropertyType;

    but now I get an error in that tangled expression jungle

    Instance property 'Role.Id' is not defined for type 'HTS.Content.Employee'

    This is still the same naming issue I'm guessing? I have no idea how to proceed.

    Tuesday, January 17, 2012 3:27 PM
  • User-330204900 posted

    if you di around in my cascading Field /Filter articles you may fine what you need.

    Tuesday, January 17, 2012 6:36 PM
  • User-1488931086 posted

    if you di around in my cascading Field /Filter articles you may fine what you need.

    dear MVP---

    Can you show us your link or sample codes in details?

    Many thanks!!!!!!!!!!!!!

    Tuesday, January 17, 2012 9:17 PM
  • User-1882961111 posted

    That's a lot to sort through for a beginner like me! I'm actually still working on my first .NET application and have not had to get too deep into LINQ.

    Is there a way to get the PK (Id) of the current Row? Maybe with reflection? Then I could just grab the child count from the DbContext.Set(childType).Where().ToList().Count  ....I think?

    Before I even knew about DD I had started writing my own version of DD in MVC w/ EF4.1 and found it easy enough to work with the DbContext. This WebForms stuff is way different it seems. But it's worth messing around with because the DD scaffolding is great.

    Wednesday, January 18, 2012 9:34 AM
  • User-330204900 posted

    if you look at this article here Cascading or Dependant Field Templates for ASP.Net 4.0 Preview then see Listing 4 for the PopulateListControl method and have alook at that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 18, 2012 9:41 AM
  • User-1882961111 posted

    Thanks! I had no idea where to start. I'm sure that will help.

    Wednesday, January 18, 2012 10:50 AM