3

MS DynamicData 的 Children.ascx.cs 文件有一个Page_Load方法可以返回一个超链接,上面写着“查看孩子”。我想将孩子的数量附加到超链接文本的末尾。下面是我的尝试。如何让超链接显示“查看儿童 - # 条目”?

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;
}
4

4 回答 4

3

其实这并不难。您可以将以下方法添加到 Children.ascx.cs 文件中:

    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 + ")";
    }
于 2012-01-26T13:58:26.630 回答
1

好吧,HyperLink1.Text ="SomeString" 应该使您的超链接文本为“SomeString”

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

应该让超链接说出你想说的话,只要 numEntries 当时是正确的数字,至少它在我的机器上是这样工作的..

您尝试的当前结果是什么?

于 2012-01-16T17:21:39.960 回答
1

我在这里找到了一个潜在的解决方案'FieldTemplates:Children.ascx:显示计数'http ://forums.asp.net/t/1466373.aspx/1

于 2012-01-17T17:59:32.797 回答
0

我有一个使用动态的非常简单的通用解决方案:

覆盖 Childrex.aspx.cs 中的 OnDataBiding 方法并使用以下代码获取子实体的数量。

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

// get the count property (this is a valid property for an EnitySet)
int count = dynamicField.Count;
于 2013-07-15T13:34:37.833 回答