
[英]How I can access to the “Text” Property of a ListView LayoutTemplate Control
[英]Access a control inside a the LayoutTemplate of a ListView
如何在ListView
控件的LayoutTemplate
中访问Control?
我需要转到litControlTitle
并设置其Text
属性。
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
有什么想法吗? 也许通过OnLayoutCreated
事件?
尝试这个:
((Literal)lv.FindControl("litControlTitle")).Text = "Your text";
完整的解决方案:
<asp:ListView ID="lv" OnLayoutCreated="OnLayoutCreated" runat="server">
<LayoutTemplate>
<asp:Literal ID="lt_Title" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
在codebehind中:
protected void OnLayoutCreated(object sender, EventArgs e)
{
(lv.FindControl("lt_Title") as Literal).Text = "Your text";
}
这种技术适用于模板布局; 使用控件的init事件:
<asp:ListView ID="lv" runat="server" OnDataBound="lv_DataBound">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" OnInit="litControlTitle_Init" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
并捕获对控件的引用,以便在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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.