简体   繁体   English

访问转发器中的项目

[英]access items in repeater

I have a repeater and I am trying to access labels inside it. 我有一个中继器,我正在尝试访问其中的标签。 Here is my method: 这是我的方法:

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

I can access inside repeater but here is the problem: I can't access the last item of repeater. 我可以访问直放站内部,但这是问题所在:我无法访问直放站的最后一项。 I put that 'n' variable to control how many times I turn inside foreach loop and I see that n always gives -1 of item numbers. 我把那个“ n”变量控制了我进入foreach循环的次数,我看到n总是给出-1的项目编号。 For example if I have 3 items in repeater, n is 2, if there is 1 item in repeater, n is 0. What am I doing wrong in here ? 例如,如果我在转发器中有3个项目,n为2,如果在转发器中有1个项目,n为0。在这里我在做什么错?

Edit: I am writing my .aspx page since it asked 编辑:我正在写我的.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>

I am not sure why you explicitly calling a different method but what you are doing can be easily done in ItemDataBound event of repeater control:- 我不确定为什么您显式调用其他方法,但是可以在中继器控件的ItemDataBound事件中轻松完成您正在做的事情:-

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

Then handler it like this:- 然后像这样处理它:

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

Please note there is no need to do any loop on your repeater items. 请注意,无需对中继器项目进行任何循环。 Repeater ItemDataBound event will fire for each item when it is bounded. 绑定项时,将为每个项目触发Repeater ItemDataBound事件。 Also, if you want to have a count simply declare a variable outside this method and increment it inside this event. 另外,如果要count只需在此方法外声明一个变量,然后在此事件内对其进行递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM