Link to home
Start Free TrialLog in
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.
Avatar of mattibutt
mattibutt
Flag of United States of America image

hi
you need to put the code to find the control before you can use it
Avatar of dexion432
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.
Avatar of tjgrindsted

ASKER

okay but how do i find it...can u plz and maybe show me how to do it for my ex.
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
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
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 !?
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 !?
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
 
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.
ASKER CERTIFIED SOLUTION
Avatar of tjgrindsted
tjgrindsted

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
found it myself