ASP.NET使用图像而不是HyperLinks

时间:2014-03-26 17:06:44

标签: c# asp.net

我有一个名为bookscategory.aspx的aspx页面,带有此标记:

<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <p>Books list</p>
    </HeaderTemplate>
    <ItemTemplate>
        <h3><asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Category") %>'></asp:Literal></h3>
        <asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("Reviews") %>' ItemType="ELibraryModel.Review">
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Item.Title%>' NavigateUrl='<%# "../books/bookdetails.aspx?bookId=" + Item.Id.ToString() %>'>
                </asp:HyperLink><br />
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

代码背后:

using (ELibraryEntities entities = new ELibraryEntities())
{
    var allbooks = from books in entities.Books.Include("Reviews")
                         orderby books.Category
                         select new { books.Category, books.Reviews};
    Repeater1.DataSource = allbooks;
    Repeater1.DataBind();
}

现在,在我的Default.aspx页面上,我有一个图书图片:

<div class="prod_img">
   <img src="../Images/asp.net_image.jpg" alt="" title="" border="0" /></a>
</div>

<div class="box_center">
        <div class="prod_title">ASP.NET Book</div>
            <p class="details">ASP.NET Book.</p>
            <a href="bookscategory.aspx" class="more">- read more -</a>
        <div class="clear"></div>
</div>

最后,当我点击此图片时,我想导航到那个确切的评论:

NavigateUrl='<%# "../books/bookdetails.aspx?ReviewId=" + Item.Id.ToString()

asp:HyperLink - s的一切正常,但我想使用图像。

我正在阅读C#和VB中的Beginning ASP.NET 4.5,在这里您可以看到所有评论http://aspnet45.planetwrox.com/Reviews/All.aspx。我想使用Images而不是HyperLinks。

1 个答案:

答案 0 :(得分:1)

HyperLink控件已经提供了此功能 - 它具有ImageUrl属性,当设置此属性时,HyperLink显示为图像:

<asp:HyperLink runat="server" ID="HyperLink1"
               NavigateUrl='<%# "../books/bookdetails.aspx?ReviewId=" + Item.Id.ToString() %>'
               ImageUrl="~/url/to/image" />