tags:

views:

222

answers:

4

I am trying to access a control inside a Repeater. The control is inside the <ItemTemplate> tag. I am using FindControl but it's always coming out Null. What am I doing wrong?

A: 

In most cases, spelling the control name wrong :) It may also be that you are searching for a control that exists within another container. Can you post your code?

JoshJordan
+2  A: 

My guess is that FindControl can only be used in record-level events such as ItemDataBound:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
 (ControlTypeCast) e.Item.FindControl("myControl")).SomeProperty = "foo";
}
devio
+1  A: 

I'm guessing that you're trying to find a control at the wrong point in the page lifecycle. The ItemDataBound event is where you need to look for it.

This example is in vb.net, but I'm sure you get the idea.

Protected Sub rp_items_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rp_items.ItemDataBound
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim someLiteral As Literal = e.Item.FindControl("someliteral")
    End If
End Sub
ScottE
This is how you would do it.
Gromer
A: 

try this

for vb.net

CType(e.Item.FindControl("myControl"), Literal).Text = "foo"

for c#

[Literal]e.item.FindControl["myControl"].Text="foo";

Mohit