中继器的 ItemDataBound 不影响某些值

ItemDataBound of repeater does not effect SOME OF THE values

我有一个中继器,里面有一些标签和一个按钮。这是我的 .aspx:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="EntityDataSourceTeklifler" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">
                    <ItemTemplate>
                        <div class="panel panel-primary">
                            <div class="panel-body">
                                <strong>Teklif No.</strong>&nbsp;<asp:Label ID="lblTeklifNo" runat="server" Text='<%#Eval("TeklifId") %>'></asp:Label><br />
                                <strong>Teklif Tarihi:</strong>&nbsp;<%#Eval("TeklifTarih") %><br />
                                <strong>Teklifi Hazırlayan:</strong>&nbsp;<%#Eval("Name") %>&nbsp;<%#Eval("Surname") %><br />
                                <strong>Firma Adı:</strong>&nbsp;<%#Eval("FirmaAdi") %><br />
                                <strong>Ürünler:</strong><br />
                                <%#Eval("TeklifSiparis") %>
                                <strong>Genel Toplam:</strong>&nbsp;<%#Eval("TeklifTutar") %>$<br />
                                <strong>Not:</strong><br />
                                <%#Eval("TeklifNot") %><br />
                                <strong>Teklif Durumu:</strong>&nbsp;<asp:Label ForeColor="Red" ID="lblApproved" runat="server" Text='<%# CheckIfApproved(Convert.ToBoolean(Eval("Approved"))) %>'></asp:Label><br /><br />
                                <asp:Button ID="btnAssignApproved" runat="server" Text="Satışa Döndü Olarak İşaretle" CssClass="btn btn-primary" CommandArgument='<%# Eval("TeklifId") %>' />
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>

还有我的代码隐藏:

protected string CheckIfApproved(bool isApproved)
    {
        string result;
        if (isApproved)
        {
            result = "Satışa Dönmüştür";
        }
        else
        {
            result = "Satışa Dönmemiştir";
        }
        return result;
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string teklifId = e.CommandArgument.ToString();
        TeklifTable teklif = entity.TeklifTable.Where(t => t.TeklifId == teklifId).FirstOrDefault();
        teklif.Approved = true;
        entity.SaveChanges();
        Page_Load(null, EventArgs.Empty);
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            Label lbl = e.Item.FindControl("lblApproved") as Label;
            Button btn = e.Item.FindControl("btnAssignApproved") as Button;
            if (lbl.Text.Equals("Satışa Dönmüştür"))
            {
                btn.Visible = false;
                lbl.ForeColor = System.Drawing.Color.Blue;
            }
            else 
            {
                lbl.ForeColor = System.Drawing.Color.Purple;
            }
        }
    }

如您所见,我的中继器根据数据库的布尔属性 'Approved' 更改标签颜色和按钮可见性。批准的属性随着按钮的点击而改变,这里没有任何问题。我正在检查批准的值是真还是假,并根据它更改标签。最后,ItemDataBound 方法根据已在 checkIfApproved 方法中更改的标签更改标签颜色和按钮可见性。

现在,这是我的问题。如您所见,我在 .aspx 中将默认标签颜色定义为红色。这应该更改为蓝色或紫色,但某些值仍为红色。这意味着 'Repeater1_ItemDataBound' 方法不会影响某些值。这是另一个有趣的点:中继器的第一项按照我的意愿受到影响,并根据标签文本显示为蓝色或紫色,但第二项出现 red.That 意味着第二项不受此方法影响。第三项生效,第四项不生效。事情是这样的。有什么想法吗?

你的代码的问题是你只检查项目是否是 Item 类型但转发器控制项目由 Item 以及 AlternatingItem 组成,即每个交替项目行为属于 ListItemTypeAlternatingItem

只需在您的 ItemDataBound 活动中添加此行:-

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
      //your code here
    }
}