1

以下是我的代码、设计和模型部分

在我的 NewsletterHistoryModel 中,我将时事通讯的一些信息和其中的新闻保存为一个列表。

我可以通过以下操作将时事通讯的信息打印到第一个转发器。但是当我尝试将新闻标题打印到其中的转发器时,就会出现错误的值。我正在尝试在第一个中继器的 ItemDataBound 中执行此操作。

儿童中继器没有正确的值。所有时事通讯都带有相同的 4 个新闻标题。

我怎么解决这个问题?

ascx.cs

   List<NewsletterHistoryModel> newsletterHistoryModel;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDataNewsLetter();
        }
    }

    public void BindDataNewsLetter()
    {

        newsletterHistoryModel = new List<NewsletterHistoryModel>();
        var dataList = NewsletterLayer.SelectListNewsletterByPublish();

        List<NewsletterDetailsModel> newsletterDetailsModel = new List<NewsletterDetailsModel>();
        if (dataList.Count > 0)
        {
            foreach (var item in dataList)
            {
                List<NewsModel> newsModels = new List<NewsModel>();
                newsletterDetailsModel = NewsletterDetailsLayer.SelectNewsLetterDetailWithNewsLetter(item.Title);
                foreach (var x in newsletterDetailsModel)
                {
                    //newsModels.Add(NewsLayer.SelectNews(x.NewsId));
                    newsModels.Add(NewsLayer.SelectNews(x.NewsId));
                }
                newsletterHistoryModel.Add(new NewsletterHistoryModel
                {
                    Id = item.Id,
                    Issue = item.Issue,
                    Year = item.Year,
                    Month = item.Month,
                    Day = item.Day,
                    PreviewLink = item.PreviewLink,
                    AttachmentUrl = item.CoverPhoto,
                    newsModels = newsModels
                });
            }

            rptNewsItems.DataSource = newsletterHistoryModel;
            rptNewsItems.DataBind();

        }
    }

    protected void rptNewsItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptNews = e.Item.FindControl("rptNews") as Repeater;
            foreach (var item in newsletterHistoryModel)
            {
                rptNews.DataSource = item.newsModels;
                rptNews.DataBind();
            }

        }

    }

ascx.designer.cs

 <asp:Repeater runat="server" ID="rptNewsItems" OnItemDataBound="rptNewsItems_ItemDataBound">
        <ItemTemplate>
            <a href="<%# Eval("PreviewLink") %>" class="card2 col-12 col-md-3">
                <h4 class="card-issue">#<%# Eval("Issue") %> - <%# Eval("Day") %> <%# Eval("Month") %> <%# Eval("Year") %></h4>
                <img class="card-img-top" src="<%# Eval("AttachmentUrl") %>">
                <div class="card-body">
                    <asp:Repeater runat="server" ID="rptNews">
                        <ItemTemplate>
                            <h4 class="card-description">#<%# Eval("Title") %></h4>
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
            </a>
        </ItemTemplate>
    </asp:Repeater>

通讯历史模型

    public int Id { get; set; }
    public string Issue { get; set; }
    public string Year { get; set; }
    public string Month { get; set; }
    public string Day { get; set; }
    public string PreviewLink { get; set; }
    public string AttachmentUrl { get; set; }
    public List<NewsModel> newsModels { get; set; }
4

1 回答 1

1

I solved the problem. Without the need for ItemDataBound. I have given the list of newsModel that I keep in the Model to the datasource of my child repeater.

   <asp:Repeater runat="server" ID="rptNewsItems" >
        <ItemTemplate>
            <a href="<%# Eval("PreviewLink") %>" class="card2 col-12 col-md-3">
                <h4 class="card-issue">#<%# Eval("Issue") %> - <%# Eval("Day") %> <%# Eval("Month") %> <%# Eval("Year") %></h4>
                <img class="card-img-top" src="<%# Eval("AttachmentUrl") %>">
                <div class="card-body">

                    <asp:Repeater runat="server" ID="rptNews" DataSource='<%# Eval("newsModels") %>'>
                        <ItemTemplate>
                            <h4 class="card-description"># <%# Eval("Title") %></h4>
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
            </a>
        </ItemTemplate>
    </asp:Repeater>
于 2021-12-07T10:43:43.830 回答