使用转发器难以显示数据

时间:2013-07-18 17:11:06

标签: asp.net

我正在尝试使用转发器显示其子类别和子子类别的购物类别。数据是绑定但它没有显示..任何人都可以帮助为什么? 这是我的代码:

.aspx文件

<asp:Repeater ID="CategoryRepeater" runat="server" OnItemDataBound="CategoryRepeater_OnItemDataBound">
    <ItemTemplate>
        <a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>'<%#Eval("CategoryName") %>></a><br />
        <asp:Repeater ID="SubCategoryRepeater" runat="server" OnItemDataBound="SubCategoryRepeater_OnItemDataBound">
            <ItemTemplate>
                    <a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>'<%#Eval("SubCategoryName") %>></a><br />
                <asp:Repeater ID="SubSubCategoryRepeater" runat="server">
                    <ItemTemplate>
                        <a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>&SubSubCategoryId=<%#Eval("SubSubCategoryId") %>'<%#Eval("SubSubCategoryName") %>></a><br />
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>

    </ItemTemplate>
</asp:Repeater>

C#代码:

protected void CategoryRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem))
    {
        DataRowView dataItem = e.Item.DataItem as DataRowView;
        int categoryId = Convert.ToInt32(dataItem["CategoryId"]);
        Repeater rp = (Repeater)e.Item.FindControl("SubCategoryRepeater");
        ds = us.SelectSubCategories(categoryId);
        rp.DataSource = ds;
        rp.DataBind();
    }
}

protected void SubCategoryRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem))
    {
        DataRowView dataItem = e.Item.DataItem as DataRowView;
        int SubCategoryId = Convert.ToInt32(dataItem["SubCategoryId"]);
        Repeater rp1 = (Repeater)e.Item.FindControl("SubSubCategoryRepeater");
        ds1 = us.SelectSubSubCategories(SubCategoryId);
        rp1.DataSource = ds1;
        rp1.DataBind();
    }
}

2 个答案:

答案 0 :(得分:1)

检查源代码以确保没有任何内容输出到页面。我想你只需要在锚标记内呈现类别名称。

这是相同的代码,简化了evals以查看正在发生的事情

<a href='Clothing.aspx?CategoryId={catid}'{name}></a>

应该是:

<a href='Clothing.aspx?CategoryId={catid}'>{name}</a>

<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>'><%#Eval("CategoryName") %></a>

在所有三个地方都犯了同样的错误。

答案 1 :(得分:1)

我认为代码中的所有内容都是正确的,但输出错误。

你这样做:

<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>'<%#Eval("SubCategoryName") %>></a><br />

但它必须是

<a href='Clothing.aspx?CategoryId=<%#Eval("CategoryId") %>&SubCategoryId=<%#Eval("SubCategoryId") %>'><%#Eval("SubCategoryName") %></a><br />

注意到我移动了'&gt;'后面&lt;%#Eval(“SubCategoryName”)%&gt;