简体   繁体   English

如何在ASP.NET ListView控件内以编程方式设置图像标记URL?

[英]How to set image Tag URLs programmatically inside of an ASP.NET ListView Control?

I know how you can set an tag's url attribute programmatically in c#, but it seems when I try to access the image element inside of a tag I cannot access it. 我知道如何在C#中以编程方式设置标签的url属性,但是当我尝试访问标签内的图片元素时,似乎无法访问它。

The is residing in the <AlternatingItemTemplate>. 驻留在<AlternatingItemTemplate>.

NOTE: I am only having this issue inside the <AlternatingItemTemplate> 注意:我只在<AlternatingItemTemplate>有此问题

Now the ListView tag is also databound.(this is probably why I cannot access, because it isn't guaranteed that it will even exist perhaps). 现在ListView标签也是数据绑定的(这可能是我无法访问的原因,因为不能保证它甚至可能存在)。

How can I get around this so that I can display my images programmatically or is there a better solution? 如何解决这个问题,以便可以以编程方式显示我的图像,或者有更好的解决方案?

Here's the source: 来源:

    <asp:ListView ID="ListView_Comments" runat="server"
DataKeyNames="ReviewID,ProductID,Rating" DataSourceID="EDS_CommentsList">
<ItemTemplate>
<tr style="background-color:#EDECB3;color: #000000;"> <td><%# Eval("CustomerName") %></td>
<td> <img src='Styles/Images/ReviewRating_d<%# Eval("Rating") %>.gif' alt="">
<br />
</td>
<td> <%# Eval("Comments") %>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#F8F8F8;"> <td><%# Eval("CustomerName") %></td>
<td> 
<img id="rateImage" src="" alt="" runat="server" />
......

Should be able to access it in the codebehind in the OnItemDatabound Event. 应该能够在OnItemDatabound事件中的代码背后访问它。 There u can check to see if its an alternating item or not then use FindControl and u should have access to it. 您可以在此处检查其是否为替代项,然后使用FindControl,并且您应该可以访问它。

Or, if your image is stored in your datasource y not just set the src using <%# Eval %> 或者,如果图像存储在数据源中,则不只是使用<%#Eval%>设置src

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM