确保在Repeater绑定数据之后再使用FindControl方法。
使用FindControl方法时,要根据控件在Repeater内的位置找到它所在的控件容器,再在容器内部查找所需控件。例如:
protected void Repater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { //找到控件所在的容器 TableRow tr = e.Item.FindControl("trID") as TableRow; if (tr != null) { //在容器内部查找所需控件 Label lblName = tr.FindControl("lblName") as Label; if (lblName != null) { lblName.Text = "Name"; } } } }
其中,“trID”是包含控件的HTML table row的ID,“lblName”是要查找的控件的ID。