1

以下のリンクは、最も基本的な形式のリストビュー複合コントロールを示していますが、これを私がやろうとしていることに拡張することはできません。

コードでリストビュー テンプレートを定義する方法

私のリストビューには、2 つのフィールドを持つ 1 つのテーブル行があります。以下に示すように、最初のフィールドには 1 つの要素が含まれ、2 番目のフィールドには 2 つの要素が含まれます。

<asp:ListView ID="lstArticle" runat="server">
    <LayoutTemplate>
            <table id="itemPlaceholder" runat="server">

            </table>
    </LayoutTemplate>
    <ItemTemplate>
    <div class="ctrlArticleList_rptStandardItem">
        <table width="100%">
            <tr>
                <td valign="top" class="ctrlArticleList_firstColumnWidth">
                    <a href="<%# GetURL(Container.DataItem) %>">
                        <%# GetImage(Container.DataItem)%>
                    </a>
                </td>
                <td valign="top">
                    <span class="ctrlArticleList_title">
                        <a href="<%# GetURL(Container.DataItem) %>">
                            <%# DataBinder.Eval(Container.DataItem, "Title") %>
                        </a>
                    </span>                            
                    <span class="ctrlArticleList_date">
                        <%# convertToShortDate(DataBinder.Eval(Container.DataItem, "ActiveDate"))%>
                    </span>                                
                    <div class="ctrlArticleList_standFirst">
                        <%# DataBinder.Eval(Container.DataItem, "StandFirst")%>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    </ItemTemplate>

したがって、これを変換するには、レイアウト テンプレートと ItemTemplate に ITemplate の InstantiateIn メソッドを使用する必要があります。データは、DataBinding イベントを使用して itemtemplate にバインドされます。

現時点での私の質問は、ItemTemplate クラスをセットアップし、上記の ListView ItemTemplate から値をバインドする方法です。これは私がこれまでに管理したものです:

private class LayoutTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            var table = new HtmlGenericControl("table") { ID = "itemPlaceholder" };
            container.Controls.Add(table);
        }
    }

    private class ItemTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            var tableRow = new HtmlGenericControl("tr");

            //first field in row
            var tableFieldImage = new HtmlGenericControl("td");
            var imageLink = new HtmlGenericControl("a");
            imageLink.ID = "imageLink";
            tableRow.Controls.Add(imageLink);

            // second field in row
            var tableFieldTitle = new HtmlGenericControl("td");
            var title = new HtmlGenericControl("a");
            tableFieldTitle.Controls.Add(title);
            tableRow.Controls.Add(tableFieldTitle);

            //Bind the data with the controls
            tableRow.DataBinding += BindData;

            //add the controls to the container
            container.Controls.Add(tableRow);
        }

        public void BindData(object sender, EventArgs e)
        {
            var container = (HtmlGenericControl)sender;
            var dataItem = ((ListViewDataItem)container.NamingContainer).DataItem;

            container.Controls.Add(new Literal() { Text = dataItem.ToString() });
        }

例として、ListView で使用されるコード ビハインドの関数の 1 つを次に示します。

        public string GetURL(object objArticle)
    {
        ArticleItem articleItem = (ArticleItem)objArticle;

        Article article = new Article(Guid.Empty, articleItem.ContentVersionID);

        return GetArticleURL(article);
    }

概要

以下を複合コントロールに変換するにはどうすればよいですか。

GetURL(コンテナ.データアイテム) GetImage(コンテナ.データアイテム)

GetURL(Container.DataItem) DataBinder.Eval(Container.DataItem, "タイトル")

convertToShortDate(DataBinder.Eval(Container.DataItem, "ActiveDate"))

DataBinder.Eval(Container.DataItem, "StandFirst")

4

1 に答える 1

0

コントロールでは、<%#バインディングを生成する必要はありません。むしろ、サーバー上でバインドされたURLを取得してリンクに割り当て、ハイパーリンクを作成して次の操作を実行できます。

var link = new HyperLink();
link.NavigateUrl = GetUrl(dataItem);
link.ImageUrl = GetImage(dataItem);

dataItemは、アイテムテンプレートbinddataメソッド内で取得する特定の行のデータアイテムです。

私は基地から離れていますか?

HTH。

于 2010-03-08T14:15:21.240 に答える