Find a Control inside ASP:Repeater
我正在尝试访问中继器内的控件。该控件位于
我做错了什么?
我的猜测是 FindControl 只能用于记录级别的事件,例如 ItemDataBound:
1 2 3 4 | protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { (ControlTypeCast) e.Item.FindControl("myControl")).SomeProperty ="foo"; } |
我猜您正试图在页面生命周期的错误位置找到控件。 ItemDataBound 事件是您需要查找的地方。
这个例子在 vb.net 中,但我相信你明白了。
1 2 3 4 5 | 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 |
1 2 3 4 5 6 7 | 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
1 2 3 4 5 6 7 8 9 10 11 | 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 |
试试这个
对于 vb.net
1 | CType(e.Item.FindControl("myControl"), Literal).Text ="foo" |
对于 c#
1 | [Literal]e.item.FindControl["myControl"].Text="foo"; |
在大多数情况下,控件名称拼写错误:) 也可能是您正在搜索存在于另一个容器中的控件。你能发布你的代码吗?