Koray Durudogan
Koray Durudogan

Reputation: 634

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. 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 ?

Edit: I am writing my .aspx page since it asked

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

Upvotes: 0

Views: 2905

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 21795

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:-

<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. Also, if you want to have a count simply declare a variable outside this method and increment it inside this event.

Upvotes: 1

Related Questions