2009-05-26 58 views
2

我试图访问Repeater内的控件。该控件位于< ItemTemplate>标签内。我正在使用FindControl,但它总是出来Null。 我在做什么错?在ASP中查找控制:中继器

+1

如何发布您的直放站样本,以便我们可以看到发生了什么? – Kev 2009-05-26 15:20:10

+0

当我粘贴时,Stackoverflow编辑器会截断代码。 – 2009-05-26 15:26:00

回答

5

我的猜测是,FindControl已只能在记录级事件,如的ItemDataBound可以使用:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    (ControlTypeCast) e.Item.FindControl("myControl")).SomeProperty = "foo"; 
} 
0

在大多数情况下,拼写控件名称错误:)也可能是您正在搜索另一个容器内存在的控件。你能发布你的代码吗?

2

我猜你试图找到在页面生命周期错点的控制。 ItemDataBound事件是您需要查找的地方。

这个例子在vb.net中,但我相信你明白了。

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 
0

尝试这个

对于vb.net

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

对于C#

[Literal]e.item.FindControl["myControl"].Text="foo"; 
0
  for (int i = 0; i <= repeater1.Items.Count - 1; i++) 
     { 
      Button delete = (Button)repeater1.Items[i].FindControl("btnDelete"); 
      delete.Visible = true; 
      Button edit = (Button)repeater1.Items[i].FindControl("btnEdit"); 
      edit.Visible = true; 
     }  

Vb.net

 For i As Integer = 0 To Repeater1.Items.Count - 1 

     Dim CmbTyp As DropDownList = DirectCast(Repeater1.Items(i).FindControl("DropDownList1"),DropDownList) 
     Dim SeatN As Label = DirectCast(Repeater1.Items(i).FindControl("label1"), Label) 

     styp = CmbTyp.SelectedItem.Text.Trim 
     sNo = SeatN.Text 



    Next