473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reference a Label in a Repeater?

On my code behind page, how can I reference a Label control that's included
in a Repeater?

In other words, given:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><asp:Label ID="Label1"
runat="server"></asp:Label></td>

How can I reference Label1.Text in my VB code? (note that Label1 does not
get repeated as it's in the header)

--
Tom Kaminski IIS MVP
http://www.iistoolshed.com/ - tools, scripts, and utilities for running IIS
http://mvp.support.microsoft.com/
http://www.microsoft.com/windowsserv...y/centers/iis/

Nov 18 '05 #1
6 4563
if you want to use it from an event of that repeater such as ItemDataBound
you can use :
If e.Item.ItemType = ListItemType.Header Then
CType(e.Item.Controls(0).FindControl("Label1"), Label).Text = "Something"
End If
if you want to access it from other method you can use:

Repeater1.Controls(0).FindControl("Label1")

Regards
Martin

"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
On my code behind page, how can I reference a Label control that's included in a Repeater?

In other words, given:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><asp:Label ID="Label1"
runat="server"></asp:Label></td>

How can I reference Label1.Text in my VB code? (note that Label1 does not
get repeated as it's in the header)

--
Tom Kaminski IIS MVP
http://www.iistoolshed.com/ - tools, scripts, and utilities for running IIS http://mvp.support.microsoft.com/
http://www.microsoft.com/windowsserv...y/centers/iis/

Nov 18 '05 #2
Hi Tom,

After you bind the repeater, you should be able to get it with

lblCntrl = Repeater1.Controls(0).FindControl("Label1")

Sample code below.

Ken
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim lblCntrl As Label
Repeater1.DataSource = CreateDataSource()
Repeater1.DataMember = "StringValue"
Repeater1.DataBind()
lblCntrl = Repeater1.Controls(0).FindControl("Label1")
lblCntrl.Text = "The new text"
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
<form id="Form1" method="post" runat="server">
<ul>
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server">My label</asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:Label id="Label3" runat="server">
<%# DataBinder.Eval(Container, "DataItem.StringValue") %>
</asp:Label>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</form>
"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
On my code behind page, how can I reference a Label control that's
included
in a Repeater?

In other words, given:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><asp:Label ID="Label1"
runat="server"></asp:Label></td>

How can I reference Label1.Text in my VB code? (note that Label1 does not
get repeated as it's in the header)

--
Tom Kaminski IIS MVP
http://www.iistoolshed.com/ - tools, scripts, and utilities for running
IIS
http://mvp.support.microsoft.com/
http://www.microsoft.com/windowsserv...y/centers/iis/


Nov 18 '05 #3
Thanks.

I must be doing something wrong as when I use this code the repeater does
not render:
Dim myCtrl As Label = Repeater1.Controls(0).FindControl("Label1")

myCtrl.Text = "XXX"

If I comment those two lines out, it renders.
"Martin Marinov" <me********@mecrossroad.bg> wrote in message
news:uE****************@TK2MSFTNGP12.phx.gbl...
if you want to use it from an event of that repeater such as ItemDataBound
you can use :
If e.Item.ItemType = ListItemType.Header Then
CType(e.Item.Controls(0).FindControl("Label1"), Label).Text = "Something"
End If
if you want to access it from other method you can use:

Repeater1.Controls(0).FindControl("Label1")

Regards
Martin

"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
On my code behind page, how can I reference a Label control that's

included
in a Repeater?

In other words, given:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><asp:Label ID="Label1"
runat="server"></asp:Label></td>

How can I reference Label1.Text in my VB code? (note that Label1 does not get repeated as it's in the header)

--
Tom Kaminski IIS MVP
http://www.iistoolshed.com/ - tools, scripts, and utilities for running

IIS
http://mvp.support.microsoft.com/
http://www.microsoft.com/windowsserv...y/centers/iis/


Nov 18 '05 #4
That's it! Thanks Ken.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
Hi Tom,

After you bind the repeater, you should be able to get it with

lblCntrl = Repeater1.Controls(0).FindControl("Label1")

Sample code below.

Ken
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim lblCntrl As Label
Repeater1.DataSource = CreateDataSource()
Repeater1.DataMember = "StringValue"
Repeater1.DataBind()
lblCntrl = Repeater1.Controls(0).FindControl("Label1")
lblCntrl.Text = "The new text"
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
<form id="Form1" method="post" runat="server">
<ul>
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server">My label</asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:Label id="Label3" runat="server">
<%# DataBinder.Eval(Container, "DataItem.StringValue") %>
</asp:Label>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</form>
"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
On my code behind page, how can I reference a Label control that's
included
in a Repeater?

In other words, given:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><asp:Label ID="Label1"
runat="server"></asp:Label></td>

How can I reference Label1.Text in my VB code? (note that Label1 does not get repeated as it's in the header)

--
Tom Kaminski IIS MVP
http://www.iistoolshed.com/ - tools, scripts, and utilities for running
IIS
http://mvp.support.microsoft.com/
http://www.microsoft.com/windowsserv...y/centers/iis/

Nov 18 '05 #5
What was the problem Tom ? are you cast the finded control to a label
control
Dim myCtrl As Label = CType(Repeater1.Controls(0).FindControl("Label1"),
Label)
Regards,
Martin

"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
That's it! Thanks Ken.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
Hi Tom,

After you bind the repeater, you should be able to get it with

lblCntrl = Repeater1.Controls(0).FindControl("Label1")

Sample code below.

Ken
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim lblCntrl As Label
Repeater1.DataSource = CreateDataSource()
Repeater1.DataMember = "StringValue"
Repeater1.DataBind()
lblCntrl = Repeater1.Controls(0).FindControl("Label1")
lblCntrl.Text = "The new text"
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
<form id="Form1" method="post" runat="server">
<ul>
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server">My label</asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:Label id="Label3" runat="server">
<%# DataBinder.Eval(Container, "DataItem.StringValue") %>
</asp:Label>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</form>
"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
On my code behind page, how can I reference a Label control that's
included
in a Repeater?

In other words, given:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><asp:Label ID="Label1"
runat="server"></asp:Label></td>

How can I reference Label1.Text in my VB code? (note that Label1 does not get repeated as it's in the header)

--
Tom Kaminski IIS MVP
http://www.iistoolshed.com/ - tools, scripts, and utilities for running IIS
http://mvp.support.microsoft.com/
http://www.microsoft.com/windowsserv...y/centers/iis/


Nov 18 '05 #6
Ken sorted it out, I need to bind the repeater first.

"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
Thanks.

I must be doing something wrong as when I use this code the repeater does
not render:
Dim myCtrl As Label = Repeater1.Controls(0).FindControl("Label1")

myCtrl.Text = "XXX"

If I comment those two lines out, it renders.
"Martin Marinov" <me********@mecrossroad.bg> wrote in message
news:uE****************@TK2MSFTNGP12.phx.gbl...
if you want to use it from an event of that repeater such as ItemDataBound
you can use :
If e.Item.ItemType = ListItemType.Header Then
CType(e.Item.Controls(0).FindControl("Label1"), Label).Text = "Something" End If
if you want to access it from other method you can use:

Repeater1.Controls(0).FindControl("Label1")

Regards
Martin

"Tom Kaminski [MVP]" <tomk (A@T) mvps (D.O.T) org> wrote in message
news:ca********@kcweb01.netnews.att.com...
On my code behind page, how can I reference a Label control that's

included
in a Repeater?

In other words, given:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td><asp:Label ID="Label1"
runat="server"></asp:Label></td>

How can I reference Label1.Text in my VB code? (note that Label1 does not get repeated as it's in the header)

--
Tom Kaminski IIS MVP
http://www.iistoolshed.com/ - tools, scripts, and utilities for

running IIS
http://mvp.support.microsoft.com/
http://www.microsoft.com/windowsserv...y/centers/iis/



Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin Cunningham | last post by:
I have a repeater with some labels in it (code below). For whatever reason the text for the label is not persisted in viewstate on the postback. Is there a trick to get this to work? Is there...
0
by: Shapper | last post by:
Hello, I have an Asp:Label inside the HeaderTemplate of an ASP:Repeater: <asp:Repeater id="myRepeater" runat="server"> <HeaderTemplate> <asp:Label id="title" runat="server"></asp:Label> ... ...
4
by: Joel | last post by:
Hey all- I'm new to asp.net so please bear with me -- I'm attempting to reference the repeater's Container.DataItem outside the repeater object. In other words -- In a seperate sub routine:...
8
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!!...
1
by: Groove | last post by:
I have a typical Repeater that contains a Template (html table). The repeater / template lists many records and in the footer, I'd like to simply SUM up the $$ amounts from all the records in the...
3
by: sck10 | last post by:
Hello, How do you reference a label inside a repeater? I tried the following and got the error: Object reference not set to an instance of an object. ...
2
by: rn5a | last post by:
Consider the following code: <script runat="server"> Sub ShowData(obj As Object, ea As EventArgs) lblDate.Text = DateTime.Now.ToString("d") lblDate.DataBind() End Sub </script> <form...
4
by: sck10 | last post by:
Hello, I am using the following Repeater control. How do I reference the label "lblCurrentYr"? Thanks, sck10 <asp:Repeater id="rptCustomerRevenue" runat="server"> <HeaderTemplate>
0
by: =?Utf-8?B?RXJpY2E=?= | last post by:
I think I know what my problem is , but I don't know how to correct it. So, if anyone could help, I would be very grateful. I am using the PagedDataSource class to implement paging in a repeater...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.