访问转发器中的项目

时间:2015-12-16 11:24:30

标签: c# asp.net repeater

我有一个转发器,我正在尝试访问其中的标签。这是我的方法:

protected void ButtonlarıTemizle()
    {
        int n = 0;
        foreach (RepeaterItem item in Repeater1.Items)
        {
            n++;
            Label lbl = item.FindControl("lblApproved") as Label;
            Button btn = item.FindControl("btnAssignApproved") as Button;
            if (lbl.Text.Equals("Satışa Dönmüştür"))
            {
                btn.Visible = false;
                lbl.ForeColor = System.Drawing.Color.Blue;
            }
        }
        Response.Write("<script lang='JavaScript'>alert('"+n+"');</script>");
    }

我可以访问内部转发器,但问题是:我无法访问转发器的最后一项。我把'n'变量控制到我在foreach循环内转了多少次,我看到n总是给出项数的-1。例如,如果我在转发器中有3个项目,则n为2,如果转发器中有1个项目,则n为0.我在这里做错了什么?

编辑:我正在写我的.aspx页面,因为它要求

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="EntityDataSourceTeklifler" OnItemCommand="Repeater1_ItemCommand">
                    <ItemTemplate>
                        <div class="panel panel-primary">
                            <div class="panel-body">
                                <strong>Teklif No.</strong>&nbsp;<%#Eval("TeklifId") %><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" CommandName="Done" CommandArgument='<%# Eval("TeklifId") %>' />
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>

1 个答案:

答案 0 :(得分:1)

我不确定为什么你明确地调用另一种方法,但你在做什么可以在转发器控制的ItemDataBound事件中轻松完成: -

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"

然后像这样处理它: -

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;
       }
   }
}

请注意,您的转发器项目无需循环。每个项目受限时,将对每个项目触发Repeater ItemDataBound事件。此外,如果您希望count只是在此方法之外声明一个变量,并在此事件中增加它。