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
'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
Thx for ur interest.
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
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>
Thx for ur interest.
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.
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
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
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_ItemD ataBound">
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.AlternatingIt em Then
Dim lbl As Label = DirectCast(e.Item.FindCont rol("PValu e"), Label)
lbl.Text = (Get the Count value from your linqdb Sub here)
End If
End Sub
In your .aspx file, add the OnItemDataBound attribute to your repeater declaration :
<asp:Repeater id="rpt" runat="server" OnItemDataBound="rpt_ItemD
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.AlternatingIt
Dim lbl As Label = DirectCast(e.Item.FindCont
lbl.Text = (Get the Count value from your linqdb Sub here)
End If
End Sub
ASKER
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 !?
I almost get it...only problem for me now.
How du i get
PValue.Text = Q.ToList.Count()
from Linq() Sub to rpt_ItemDataBound !?
ASKER
okay i can get this to work in ITEMS
But what do i do if its in my Footer i need the info !?
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
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.FindCont rol("PValu e"), 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
Protected Sub rpt_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
Dim lbl As Label = DirectCast(e.Item.FindCont
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
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.
and it if i have a label in items and footer no value at all.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
found it myself
you need to put the code to find the control before you can use it