DynamicData - 如何在 Children.ascx.cs FieldTemplate 中显示子项计数?

发布于 2024-12-27 10:15:31 字数 646 浏览 1 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

默嘫て 2025-01-03 10:15:31

其实这并不难。您可以将以下方法添加到 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 + ")";
    }

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 + ")";
    }
相权↑美人 2025-01-03 10:15:31

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

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

应该使超链接说出您想要说的内容,只要 numEntries 当时是正确的数字,至少它在我的设备上是这样工作的机器..

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

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?

一页 2025-01-03 10:15:31

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

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

颜漓半夏 2025-01-03 10:15:31

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

重写 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文