1

私はネストされたリスト ビューを持っています。目標は、外側のリスト ビューから ID を取得し、それを以下のメソッドに渡すことCurrentCategoryIdです。私が読んだことから、これはイベントハンドラーで行う必要がありますが、OnItemDataBoundそれがどのように機能するかを理解するのに苦労していますか?

ご不明な点がございましたら、お知らせください

<asp:ListView ID="ListView1"
    ItemType="E_Store_Template.Models.Category"
    runat="server"
    SelectMethod="GetCategories"
    OnItemDataBound="brandList_ItemDataBound">
    <ItemTemplate>
        <ul>
            <li style="list-style-type: none; text-align: left;">
                <b style="font-size: large; font-style: normal">
                    <a href="<%#: GetRouteUrl("ProductsByCategoryRoute", new {categoryName = Item.CategoryName}) %>">
                        <%#: Item.CategoryName %>
                    </a>
                    <asp:ListView ID="brandList" runat="server" ItemType="E_Store_Template.Models.Brand">
                        <ItemTemplate>
                            <ul style="list-style-type: none; text-align: left;">
                                <li>
                                    <a href="<%#: GetRouteUrl("ProductsByCatBrandRoute", new {brandName = Item.BrandName}) %>">
                                        <%#: Item.BrandName %>
                                    </a>
                                </li>
                            </ul>
                        </ItemTemplate>
                    </asp:ListView>
                </b>
            </li>
        </ul>
    </ItemTemplate>
</asp:ListView>

イベントハンドラーはこのようにする必要があると思います

protected List<Brand> brandList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewDataItem dataItem = (ListViewDataItem)e.Item;

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Category mydata = (Category)dataItem.DataItem;
        int CurrentCategoryId = mydata.CategoryID;
        var query = _db.Products.Where(p => p.Category.CategoryID == CurrentCategoryId).Select(p => p.Brand).Distinct();
        return query.ToList<Brand>();
    }
    else
    {
        return null;
    }
}

しかし、私にエラーを与えます:

'System.Collections.Generic.List<E_Store_Template.Models.Brand> E_Store_Template.SiteMaster.brandList_ItemDataBound(object, System.Web.UI.WebControls.ListViewItemEventArgs)' has the wrong return type
4

1 に答える 1

1

ItemDataBoundはデータを返すようには設計されておらず、イベントを処理するように設計されています。この場合、イベントはカテゴリと、子イベントとして発生する必要のあるすべてのものをレンダリングします。適切なアクションは、親アイテムから子ListViewを取得し、それにデータをバインドすることです。マークアップ:

<asp:ListView ID="categoryList" runat="server"
    OnItemDataBound="brandList_ItemDataBound" ItemType="E_Store_Template.Models.Category" SelectMethod="GetCategories">
        <LayoutTemplate>
            <ul style="list-style-type: none;">
                <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
            </ul>
        </LayoutTemplate>
        <ItemTemplate>
            <li style="text-align: left;">
                <b style="font-size: large; font-style: normal">
                    <a href="<%#: GetRouteUrl("ProductsByCategoryRoute", new {categoryName = Item.CategoryName}) %>">
                        <%#: Item.CategoryName %>
                    </a>
                </b>
                <asp:ListView ID="brandList" runat="server">
                    <LayoutTemplate>
                        <ul style="list-style-type: none; text-align: left;">
                            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                        </ul>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <li>
                            <a href="<%#: GetRouteUrl("ProductsByCatBrandRoute", new {brandName = Item.BrandName}) %>">
                                <%#: Item.BrandName %>
                            </a>
                        </li>
                    </ItemTemplate>
                </asp:ListView>
            </li>
        </ItemTemplate>
    </asp:ListView>

背後にあるコード:

protected void brandList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewDataItem dataItem = (ListViewDataItem)e.Item;

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Category mydata = (Category)dataItem.DataItem;

        int CurrentCategoryId = mydata.CategoryID;
        var query = _db.Products.Where(p => p.Category.CategoryID == CurrentCategoryId).Select(p => p.Brand).Distinct();

        ListView brandList = (ListView)e.Item.FindControl("brandList");
        brandList.DataSource = query.ToList<Brand>();
        brandList.DataBind();
    }
}

PlaceHoldersでLayoutTemplatesを追加したので、ListViewはレコードごとにタグを繰り返さないようにしました。また、太字のタグセットの子としてブロック要素(順序付けされていないリスト)を含めるべきではないため、終了タグを移動しました。

1つ(または2つ)のSQLクエリですべてのカテゴリとブランドを取得してから、適切なCategoryIDでブランドデータリストをフィルタリングすることを検討します。Brandsデータリストをページレベルの変数として保存しますが、SessionまたはViewStateにロードせず、ページのレンダリング中にのみ必要になります。

于 2013-02-26T09:04:22.843 に答える