如何在数据列表视图中给出href值?

时间:2011-12-23 10:05:46

标签: c# javascript asp.net

我正在使用灯箱在同一页面上放大显示图像。我已经在数据列表视图中定义了一个href标签来获取图像链接,当用户点击时会获得放大thumbinal。谁能告诉我如何给出href值?  我的代码在这里。

<asp:DataList ID="DataList1" runat="server" DataKeyField="ImageID" 
                    DataSourceID="SqlDataSource1" RepeatColumns="3">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" 
                            ImageUrl='<%# Eval("ImageURL","~/Images/{0}") %>' BorderColor="#009900" 
                            BorderStyle="Groove" BorderWidth="5px" Height="300px" Width="300px" />
                         <a href='<%# Eval("ImageURL","~/Images/{0}") %>' rel="lightbox" title='<%# Eval("ImageURL","~/Images/{0}") %>'><asp:DataList ID="DataList1" runat="server" DataKeyField="ImageID" 
                    DataSourceID="SqlDataSource1" RepeatColumns="3">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" 
                            ImageUrl='<%# Eval("ImageURL","~/Images/{0}") %>' BorderColor="#009900" 
                            BorderStyle="Groove" BorderWidth="5px" Height="300px" Width="300px" />
                         <a href='<%# Eval("ImageURL","~/Images/{0}") %>' rel="lightbox" title='<%# Eval("ImageURL","~/Images/{0}") %>'><img src='<%# Eval("ImageURL","~/Images/{0}") %>' />"</a>
                        <br />
                        ImageID:
                        <asp:Label ID="ImageIDLabel" runat="server" Text='<%# Eval("ImageID") %>' />
                        <br />
                        UploadedBy:
                        <asp:Label ID="UploadedByLabel" runat="server" Text='<%# Eval("UploadedBy") %>' />
                        <br />
                        <br />
                    </ItemTemplate>
                </asp:DataList></a>
                        <br />
                        ImageID:
                        <asp:Label ID="ImageIDLabel" runat="server" Text='<%# Eval("ImageID") %>' />
                        <br />
                        UploadedBy:
                        <asp:Label ID="UploadedByLabel" runat="server" Text='<%# Eval("UploadedBy") %>' />
                        <br />
                        <br />
                    </ItemTemplate>
                </asp:DataList>

预先谢谢!

2 个答案:

答案 0 :(得分:0)

对于以“〜”开头的路径来解决我很确定你已经将runat =“server”添加到控件中,以便它由.NET作为.NET控件处理。否则HREF路径将只有“〜/ images / {0}”,这在html中没有意义..所以

 <a href='<%# Eval("ImageURL","~/Images/{0}") %>' rel="lightbox" title='<%# Eval("ImageURL","~/Images/{0}") %>'>

变为

 <a href='<%# Eval("ImageURL","~/Images/{0}") %>' rel="lightbox" title='<%# Eval("ImageURL","~/Images/{0}") %>' runat="server">

应该使您的href路径正确解析

与图像相同

 <img src='<%# Eval("ImageURL","~/Images/{0}") %>' runat="server" />

答案 1 :(得分:0)

使用带有ImageURL和NavigateUrl的asp:超链接。

请参阅http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.aspx

<asp:HyperLink id="hyperlink1" 
              ImageUrl="images/pict.jpg"
              NavigateUrl="http://www.microsoft.com"
              Text="Microsoft Official Site"
              Target="_new"
              runat="server"/>