我有一个博客的代码,它在数据列表中嵌套转发器控件。
我正在尝试将转发器控件图像的alt标签分配给通过datalist发布的帖子的标题,但是,它不会让我这样,因为它有不同的数据源。
关于我如何做到这一点的任何建议?
这是我的代码:
<asp:DataList runat="server" ID="DataList1" ItemStyle-CssClass="row1BackgroundBlock" OnItemDataBound="DataList1_ItemDataBound" >
<ItemTemplate>
<!-- <%#Eval("PostId")%> post -->
<div class="dateBox"><asp:Label ID="Label1" runat="server" ForeColor="#ffffff" Font-Bold="False" Text='<%# Eval("PublicationDate","{0:dd/MM/yyyy}") %>' CssClass="postmetadata"></asp:Label></div>
<div class="roundPanelStack">
<h2><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Engine2010.Blog.BlogLinksManager.GetFullPostsLinks((int)Eval("PostId"), (object)Eval("FURL")) %>'><%# Eval("Title") %></asp:HyperLink></h2>
<ul><li class="postBy">Posted in <a href="<%#CommonFunctions.GetTreeURL(146).ToString() + "?categoryid=" + Eval("CategoryID") %>"><%#Eval("CategoryName")%></a> by <%#Eval("Author")%></li><li class="last">{<%# Engine2010.Blog.PostComment.GetAllByPostId(int.Parse(Eval("PostId").ToString()), 766).Rows.Count.ToString()%> comments}</li></ul>
<div class="thumbWindow">
<div style="border:7px solid #fff;display:block;height:95px;left:1px;position:absolute;top:1px;width:95px;z-index:5;"></div>
<asp:Repeater ID="repImgs" runat="server">
<ItemTemplate>
<img class="thumb_view" src="/uploads/images/Blog/<%# Eval("Directory").ToString().Substring(Eval("Directory").ToString().LastIndexOf("\\")+1) %>/<%# Eval("Name") %>" alt="" border="0" width="143" height="143" />
</ItemTemplate>
</asp:Repeater>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
<img src="/includes/images/general/default.jpg" alt="Default" border="0" width="110" height="110" />
</asp:PlaceHolder>
</div>
<p>
<asp:Label ID="Label2" runat="server" Text='<%#CommonFunctions.Remove_HTML_Tags(Eval("Description").ToString()) %>' CssClass="entry"></asp:Label>
</p>
<div class="linksMore">
<asp:HyperLink ID="FullStoryLink" runat="server" NavigateUrl='<%# Engine2010.Blog.BlogLinksManager.GetFullPostsLinks((int)Eval("PostId"), (object)Eval("FURL")) %>' CssClass="fullstory">Read Full Post</asp:HyperLink><a class="spacer"> // </a><asp:HyperLink ID="CommentsLink" runat="server" NavigateUrl='<%# Engine2010.Blog.BlogLinksManager.GetFullPostsLinks((int)Eval("PostId"), (object)Eval("FURL")) + "#comment" %>' CssClass="comments">Comment</asp:HyperLink>
</div>
</div>
<!-- /first post -->
</ItemTemplate>
</asp:DataList>
后端:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater repImages = (Repeater)e.Item.FindControl("repImgs"); // Child repeater with images for room
string amenText = ((DataRowView)e.Item.DataItem).Row.ItemArray[7].ToString();
DirectoryInfo folderBr = new DirectoryInfo(Server.MapPath("~/uploads/images/Blog/") + amenText);
// List<Engine2010.ImageObj> imageObjs = folderBr.GetImagesInPath();
if (folderBr.Exists)
{
repImages.DataSource = folderBr.GetFiles("thumb*.*"); // Bind room images to child repeater
repImages.DataBind();
if (repImages.Items.Count == 0)
{
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
}
else {
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
}
}
答案 0 :(得分:1)
在绑定repImages
repater控件后,在DataList1的 ItemDataBound 事件中,使用RepeaterItem
遍历每个项目,然后找到你的img控件并在alt标签中指定标题像这样 -
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater repImages = (Repeater)e.Item.FindControl("repImgs");
string amenText = ((DataRowView)e.Item.DataItem).Row.ItemArray[7].ToString();
DirectoryInfo folderBr = new DirectoryInfo(Server.MapPath("~/uploads/images/Blog/") + amenText);
if (folderBr.Exists)
{
repImages.DataSource = folderBr.GetFiles("thumb*.*"); // Bind room images to child repeater
repImages.DataBind();
if (repImages.Items.Count == 0)
{
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
// Iterate thru each item of your child repeate control.
foreach (RepeaterItem repeaterItem in repImages.Items)
{
if (repeaterItem.ItemType == ListItemType.Item ||
repeaterItem.ItemType == ListItemType.AlternatingItem)
{
((HtmlImage)(repeaterItem.FindControl("imgId"))).Alt = amenText; // Assume amenText is the title of your post
}
}
}
else
{
PlaceHolder placeholder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
placeholder1.Visible = true;
}
}
}
祝你好运......