Reference control in Repeater Control

S

sck10

Hello,

I am using the following Repeater control. How do I reference the label
"lblCurrentYr"?

Thanks, sck10


<asp:Repeater id="rptCustomerRevenue" runat="server">
<HeaderTemplate>
<table class="tblOutline" style="width:100%" border="1px">
<tr>
<th style="width:37%;">Service</th>
<th style="width:12%;"><asp:Label ID="lblCurrentYr" runat="server"
/></th>
<th style="width:11%;"><asp:Label ID="lblYearMinus01" runat="server"
/></th>
<th style="width:15%;"><asp:Label ID="lblYearMinus02" runat="server"
/></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:37%; text-align:left;"><%#Eval("Service")%></td>
<td style="width:12%; text-align:right;"><%#Eval("CurrentYrMinus00",
"{0:C0}")%></td>
<td style="width:11%; text-align:right;"><%#Eval("CurrentYrMinus01",
"{0:C0}")%></td>
<td style="width:15%; text-align:right;"><%#Eval("CurrentYrMinus02",
"{0:C0}")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
 
E

Eliyahu Goldin

Handle ItemCreated or ItemDataBound event. Use e.Item.ItemType to tell the
Header item. Use e.Item.FindControl("lblCurrentYr") to locate the label
inside the header item.
 
S

Steven Cheng[MSFT]

Hello Steve,

For your scenario, there are two possible means:

1. Directly access the repeater header through controls collection by
index(this is not quite recommended since index is not guranteed to work
between different version). e.g.

#header row is the first childcontrol in repeater's Controls collection
====================
protected void Button1_Click(object sender, EventArgs e)
{
Label lbl = Repeater1.Controls[0].FindControl("lblCurrentYr") as
Label;

if (lbl != null)
{
lbl.Text = "new text " + DateTime.Now.Ticks;
}



}
=========================

2. As Eliyahu has mentioned, one place we can access repeater's header(or
footer)'s controls is the ItemCreated, you can get the label in header in
that event and store it into a page variable so that you can use it later.
e.g.

========in page code behind=========
public partial class Part1_repeaterpage : System.Web.UI.Page
{
Label _repeaterLabel = null;
................


protected void Button1_Click(object sender, EventArgs e)
{

Response.Write("<br/>label value: " + _repeaterLabel.Text);

}


protected void Repeater1_ItemCreated(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label lbl = e.Item.FindControl("lblCurrentYr") as Label;

if (lbl != null)
{
_repeaterLabel = lbl;
}
}
}
.....................
}

=============================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sck10

Thanks Eliyahu,

sck10


Eliyahu Goldin said:
Handle ItemCreated or ItemDataBound event. Use e.Item.ItemType to tell the
Header item. Use e.Item.FindControl("lblCurrentYr") to locate the label
inside the header item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


sck10 said:
Hello,

I am using the following Repeater control. How do I reference the label
"lblCurrentYr"?

Thanks, sck10


<asp:Repeater id="rptCustomerRevenue" runat="server">
<HeaderTemplate>
<table class="tblOutline" style="width:100%" border="1px">
<tr>
<th style="width:37%;">Service</th>
<th style="width:12%;"><asp:Label ID="lblCurrentYr" runat="server"
/></th>
<th style="width:11%;"><asp:Label ID="lblYearMinus01" runat="server"
/></th>
<th style="width:15%;"><asp:Label ID="lblYearMinus02" runat="server"
/></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:37%; text-align:left;"><%#Eval("Service")%></td>
<td style="width:12%; text-align:right;"><%#Eval("CurrentYrMinus00",
"{0:C0}")%></td>
<td style="width:11%; text-align:right;"><%#Eval("CurrentYrMinus01",
"{0:C0}")%></td>
<td style="width:15%; text-align:right;"><%#Eval("CurrentYrMinus02",
"{0:C0}")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top