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: Displaying Count' : http://forums.asp.net/t/1466373.aspx/1

于 2012-01-17T17:59:32.797 に答える
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;
于 2013-07-15T13:34:37.833 に答える