中继器的ItemDataBound不会影响某些值

本文关键字:影响 ItemDataBound 中继器 | 更新日期: 2023-09-27 18:26:58

我有一个中继器,里面有一些标签和按钮。这是我的.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"更改标签颜色和按钮可见性。点击按钮即可更改已批准的属性,此处没有任何问题。我正在检查approved值是真是假,并根据它更改标签。最后,ItemDataBound方法根据checkIfApproved方法中更改的标签更改标签颜色和按钮可见性。

现在,这是我的问题。正如你所看到的,我在.aspx中将默认标签颜色定义为红色。这应该更改为蓝色或紫色,但某些值保持为红色。也就是说"Repeater1_ItemDataBound"方法不会影响某些值。这里还有一个有趣的点:中继器的第一个项目按照我的意愿生效,并根据标签文本显示为蓝色或紫色,但第二个项目变为红色。这意味着第二个项不会受到这种方法的影响。第三项生效,第四项不生效。事情是这样的。有什么想法吗?

中继器的ItemDataBound不会影响某些值

代码的问题是,您只检查该项是否为Item类型,但中继器控制项由ItemAlternatingItem组成,即每个交替项行为都属于ListItemTypeAlternatingItem

只需在ItemDataBound事件中添加以下行:-

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