Label Inside Repeater

I have a label inside of a data repeater. When I reference it in my code, it says it is not defined. If I add the repeater name like -


MyRepeater.MyLabel.Text = "some value"

it says that the label is not a member of the repeater.

You can use FindControl.

What are you trying to do exactly? Why would you use a Repeater control if you are simply trying to write text out onto the screen via a Label control.

I have repeater writing out items in a database. Within the header template I have column headings. I need to have it so the text of the column headings be dynamic, now.

I tried this -


Dim lbl1 as Label = Page.FindControl("Label1")
lbl1.Text = "somevalue"

but I am now getting an ‘Object reference not set to an instance of an object.’

Got it. Take a look at this link…it solves your problem:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskreferencingcontrolsinwebformspages.asp

Yes, I had tried casting as well, but got the same error. I removed the header template and just added a table above the repeater. It works fine, althogh, the table is also set as a server control so in principal it should’ve worked the same but doesn’t.

Maybe something like

Dim vRepeater as repeater
Dim vItem As RepeaterItem
Dim vLabel as label
vReapeater = Me.FindControl(“RepeaterID”)
ForEach vItem In vReapeater.Items
vLabel = vItem.FindControl(“TreeTable”)
vLabel.text = “kdjvdfvdfh”
Next

You need to do this in the ItemDataBound event handler. Here is what the code would look like in C#, maybe Zak can translate to VB.

If your repeater was named rpProducts and the label in the header was named foo


 private void rpProducts_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
 {
 	if(e.Item.ItemType == ListItemType.Header)
 	{
 		Label lbl = e.Item.FindControl("foo") as Label;
 		lbl.Text = "My Header Text";
 	}
 }
 
 

When the repeater is bound to data, the itemdatabound event runs for each item the datasource but also once for the header, footer, etc… types of items. Basically each type in the ListItemType enumeration.

I think he said he tried that and it didn’t work. At any rate…here’s the translated code.
When did I become the VB.NET translator guy? :xeye:

Sub rpProducts_ItemDataBound(s As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)
 	If (e.Item.ItemType = ListItemType.Header) Then
 		Dim lbl As Label = e.Item.FindControl("foo")
 		lbl.Text = "My Header Text"
 	End If
End Sub

Once, again…untested but fairly accurate.

I am not sure where he said he tried that, mostly he just tried to access it from the Page_Load or some other event, but never the ItemDataBound event. Which is the only place as far as I know that you can access it from. Maybe the ItemCreated event but not sure.

You are just the man, I dunno, I suppose I could have translated it to VB.NET, but I figured someone who uses it alot could do it a lot faster.

Dah…how do you know I use VB.NET alot? Is everything I post in VB.NET? :confused:

You wrote a book called
Build Your Own ASP.NET Website Using C# & VB.NET
I figured translating between the two for you would be easy. Guess I was mistaken. Sorry.

Good point…although I have to admit…I’ve been programming in VB/VBScript since 1996…so it comes more natural. C# I’ve only been doing since its inception and I’ve never been much of a C/C++ guy. To tell you the truth, I translated all of the VB.NET to C# but then Kevin Yank went through and brought all of the C# up to “modern standards”. I’m so not hip!!

For future reference, here’s a nice little C# to VB.NET converter:

http://www.kamalpatel.net/ConvertCSharp2VB.aspx

Thanks for the help, but I am getting the same error as before which was also inside of the OnItemDataBound - Object reference not to set to an instance of an object.


Sub rptOptions_OnItemDatabound(sender As Object, e As RepeaterItemEventArgs)
 If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
  'add labels dynamically
  Dim lbl As Label = e.Item.FindControl("Label1")
  lbl.Text = "Test"
 End If
End Sub
<asp:repeater ID="rptWishList1" runat="server">
 <itemtemplate>
  <outertable...>
  <asp:repeater ID="rptOptions" runat="server" DataSource='<%# GetSrc(DataBinder.Eval(Container.DataItem,"ProductID")) %>' OnItemDataBound="rptOptions_OnItemDatabound">
   <headertemplate>
	<table width="500" border="0" cellspacing="0" cellpadding="3">
	 <tr bgcolor="#D4D8DB">
	  <td>&nbsp;</td>
	  <td><asp:label ID="Label1" runat="server" /></td>
	  <td><asp:label ID="Label2" runat="server" /></td>
	  <td><asp:label ID="Label3" runat="server" /></td>
	 </tr>
	</table>
   </headertemplate>
   <itemtemplate>
	<itemtablestuff...>
   </itemtemplate>
   <footertemplate>
	<br>
   </footertemplate>
  </asp:repeater>
 </itemtemplate>
</asp:repeater>

I would think it has something to do with the fact that I need to bind this in the header, not the item/altItem templates.

Yeah your if clause is set to if you are not acessing a header or a footer type.


 Sub rptOptions_OnItemDatabound(sender As Object, e As RepeaterItemEventArgs)
 If e.Item.ItemType = ListItemType.Header Then
  'add labels dynamically
  Dim lbl As Label = e.Item.FindControl("Label1")
  lbl.Text = "Test"
 End If
End Sub 
 

Thanks! I am either going blind or just so used to typing that code I don’t even notice it. I ran into one other problem but was able to work-around it. It’s a little tricky considering I want the header values based on the item value but the header value comes first.