Avatar of tjgrindsted
tjgrindsted asked on

Cant show PValue label inside a repeater.

Hi im new to this so hope someone can help me.
Im not knowing all the things so somethings i need to learn.

I have a problem, im showing some data in a repeater and have made a Count() in my codebehind, but it not showed inside the repeater, if i drag the asp:label outside the repeater i get a value.
I have search google for FindControl AND Repeater, but i dont under stand it...can someone plz. help me with my problem so i can see what to do in my code.

This is my CodeBehind
    Public Sub linqdb()

        Dim DBconn As New GetProductListDataContext()
        Dim Q = _
        From p In DBconn.GetTable(Of Product)() _
        Where p.IsEnabled = "1" _
             Order By p.PName Descending _
        Select p

        MyRep.DataSource = Q
        MyRep.DataBind()
        PValue.Text = Q.ToList.Count()

    End Sub

Open in new window

Here its the PValue i need in my Label, if the label is inside the repeater i get an error:
'PValue' is not declared. It may be inaccessible due to its protection level.
If i drag the label outside the repeater the error go away and it works.

But i need to show the PValue inside the repeater.

My label code inside the repeater is
<span id="" class="">Ialt <asp:label id="PValue" runat="server"/> Produkter</span>

Open in new window


Thx for ur interest.
ASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
tjgrindsted

8/22/2022 - Mon
mattibutt

hi
you need to put the code to find the control before you can use it
dexion432

In thw grid row_:bound event you need to use findcontrol.

a control within a grid or repeater is not accessible from you codebehind file as it is in a row/cell/... template far away from the root of the control tree which is accessed directly in your codebehind file.
ASKER
tjgrindsted

okay but how do i find it...can u plz and maybe show me how to do it for my ex.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
dexion432

create a rowdatabound function for the repeater

and try this:
Label PValue =  e.Row.FindControl("PValue")

and after this line you can set the text
BurnieP

Since your Label is inside a repeater, you need to set it's value in the OnItemDataBound event.

In your .aspx file, add the OnItemDataBound attribute to your repeater declaration :

<asp:Repeater id="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">

Server side, add the corresponding event :

Protected Sub rpt_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
      If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbl As Label = DirectCast(e.Item.FindControl("PValue"), Label)
                              lbl.Text = (Get the Count value from your linqdb Sub here)
      End If
End Sub
ASKER
tjgrindsted

Hi BurnieP

I almost get it...only problem for me now.
How du i get
PValue.Text = Q.ToList.Count()

from Linq() Sub to rpt_ItemDataBound !?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
tjgrindsted

okay i can get this to work in ITEMS

    Public Sub linqdb()

        Dim DBconn As New GetProductListDataContext()
        Dim Q = _
        From p In DBconn.GetTable(Of Product)() _
        Where p.IsEnabled = "1" _
             Order By p.PName Descending _
        Select p

        MyRep.DataSource = Q
        MyRep.DataBind()

         For Each ctl As Control In MyRep.Items
            CType(ctl.FindControl("PValue"), Label).Text = Q.ToList.Count()
        Next

    End Sub

Open in new window


But what do i do if its in my Footer i need the info !?
BurnieP

If it is in your Footer, than change the If like below.

Protected Sub rpt_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
      If e.Item.ItemType = ListItemType.Footer  Then
            Dim lbl As Label = DirectCast(e.Item.FindControl("PValue"), Label)
                              lbl.Text = linqdb()
      End If
End Sub

You can change your linq db Sub to return the value.  And call it.

Public Function linqdb() AS Integer

        Dim DBconn As New GetProductListDataContext()
        Dim Q = _
        From p In DBconn.GetTable(Of Product)() _
        Where p.IsEnabled = "1" _
             Order By p.PName Descending _
        Select p

        MyRep.DataSource = Q
        MyRep.DataBind()
        linqdb = Q.ToList.Count()

    End Sub

    End Sub
 
ASKER
tjgrindsted

hi if i do that then the site run fine in debug (F5) but then Asp.label dont get any value.
and it if i have a label in items and footer no value at all.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
tjgrindsted

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
tjgrindsted

found it myself