在嵌套转发器中使用数据列表项

本文关键字:数据 列表 嵌套 转发器 | 更新日期: 2023-09-27 18:25:08

我有一个博客的代码,它在数据列表中嵌套了一个中继器控件。

我正试图将中继器控制图像的alt标签分配为通过数据列表发布的帖子的标题,但它不允许我这样做,因为它有不同的数据源。

关于我该怎么做,有什么建议吗?

这是我的代码:

<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">&nbsp;//&nbsp;</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;
          }
    }
}

在嵌套转发器中使用数据列表项

在绑定repImages repter控件后,在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;
        }
    }
}

祝你好运。。。