mbi*_*ard 119 asp.net controls repeater findcontrol
我想知道如何在Asp.Net Repeater控件的HeaderTemplate或FooterTemplate中找到控件.
我可以在ItemDataBound事件上访问它们,但我想知道如何获取它们(例如,在页眉/页脚中检索输入的值).
注意:我在找到答案之后在这里发布了这个问题,以便我记住它(也许其他人可能会觉得这很有用).
mbi*_*ard 170
正如评论中所指出的,这仅适用于您使用DataBound转发器后.
要在标题中查找控件:
lblControl = repeater1.Controls[0].Controls[0].FindControl("lblControl");
Run Code Online (Sandbox Code Playgroud)
要在页脚中查找控件:
lblControl = repeater1.Controls[repeater1.Controls.Count - 1].Controls[0].FindControl("lblControl");
Run Code Online (Sandbox Code Playgroud)
public static class RepeaterExtensionMethods
{
public static Control FindControlInHeader(this Repeater repeater, string controlName)
{
return repeater.Controls[0].Controls[0].FindControl(controlName);
}
public static Control FindControlInFooter(this Repeater repeater, string controlName)
{
return repeater.Controls[repeater.Controls.Count - 1].Controls[0].FindControl(controlName);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 50
好的解决方案
您可以在ItemCreated事件中检查项目类型:
protected void rptSummary_ItemCreated(Object sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Footer) {
e.Item.FindControl(ctrl);
}
if (e.Item.ItemType == ListItemType.Header) {
e.Item.FindControl(ctrl);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100884 次 |
| 最近记录: |