访问ListView的LayoutTemplate内的控件
如何在ListView
控件的LayoutTemplate
中访问Control?
我需要到litControlTitle
并设置其Text
属性。
有什么想法吗? 也许是通过OnLayoutCreated
事件?
试试这个:
((Literal)lv.FindControl("litControlTitle")).Text = "Your text";
完整的解决方案:
在codebehind中:
protected void OnLayoutCreated(object sender, EventArgs e) { (lv.FindControl("lt_Title") as Literal).Text = "Your text"; }
这种技术适用于模板布局; 使用控件的init事件:
并捕获对控件的引用,以便在ListView的DataBound事件中使用代码隐藏(例如):
private Literal litControlTitle; protected void litControlTitle_Init(object sender, EventArgs e) { litControlTitle = (Literal) sender; } protected void lv_DataBound(object sender, EventArgs e) { litControlTitle.Text = "Title..."; }
对于嵌套LV循环:
void lvSecondLevel_LayoutCreated(object sender, EventArgs e) { Literal litText = lvFirstLevel.FindControl("lvSecondLevel").FindControl("litText") as Literal; litMainMenuText.Text = "This is test"; }
如果你需要VB版本,这里就是
Dim litControl = CType(lv.FindControl("litControlTitle"), Literal) litControl.Text = "your text"