C# 访问 ListView 的 LayoutTemplate 中的控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/433846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access a control inside a the LayoutTemplate of a ListView
提问by craigmoliver
How do I access a Control in the LayoutTemplate
of a ListView
control?
如何访问在一个控制LayoutTemplate
一个的ListView
控制?
I need to get to litControlTitle
and set its Text
attribute.
我需要获取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>
Any thoughts? Perhaps via the OnLayoutCreated
event?
有什么想法吗?也许通过OnLayoutCreated
事件?
采纳答案by tanathos
Try this:
尝试这个:
((Literal)lv.FindControl("litControlTitle")).Text = "Your text";
回答by Vindberg
The complete solution:
完整的解决方案:
<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>
In codebehind:
在代码隐藏中:
protected void OnLayoutCreated(object sender, EventArgs e)
{
(lv.FindControl("lt_Title") as Literal).Text = "Your text";
}
回答by JonH
This technique works for template layout; use the init event of the control:
这种技术适用于模板布局;使用控件的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>
And capture a reference to the control for use in the code-behind (e.g) in the ListView's DataBound event:
并捕获对控件的引用,以便在 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...";
}
回答by Dheeraj Palagiri
For Nested LV Loop:
对于嵌套 LV 循环:
void lvSecondLevel_LayoutCreated(object sender, EventArgs e)
{
Literal litText = lvFirstLevel.FindControl("lvSecondLevel").FindControl("litText") as Literal;
litMainMenuText.Text = "This is test";
}
回答by Jeff
In case you need the VB version, here it is
如果您需要 VB 版本,这里是
Dim litControl = CType(lv.FindControl("litControlTitle"), Literal)
litControl.Text = "your text"