我正在尝试访问Repeater中的控件。该控件位于< ItemTemplate>内。标签。我正在使用FindControl,但它总是出来Null。 我做错了什么?
答案 0 :(得分:5)
我的猜测是,FindControl只能用于记录级事件,例如ItemDataBound:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
(ControlTypeCast) e.Item.FindControl("myControl")).SomeProperty = "foo";
}
答案 1 :(得分: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
答案 2 :(得分:0)
在大多数情况下,拼写控件名称错误:)也可能是您正在搜索另一个容器中存在的控件。你可以发布你的代码吗?
答案 3 :(得分:0)
试试这个
对于vb.net
CType(e.Item.FindControl("myControl"), Literal).Text = "foo"
对于c#
[Literal]e.item.FindControl["myControl"].Text="foo";
答案 4 :(得分: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