问题描述
实际上,我正在使用 ASP.NET 和 C# 开发 Web 模板.我在 usercontrol
页面和 ItemTemplate
中有一个 listview
我有一个 PlaceHolder
如下:
actually, i'm developing a web template using ASP.NET and C#.
i have a listview
in a usercontrol
page and inside the ItemTemplate
i have a PlaceHolder
as below:
<asp:PlaceHolder ID="ph_Lv_EditModule" runat="server"> </asp:PlaceHolder>
我想从后面的代码中访问这个 PlaceHolder
,我使用了以下不同的方法,但我无法访问它.
i want to access to this PlaceHolder
from code behind and i have use different method as below but i couldn't access it.
PlaceHolder ph_Lv_EditModule = (PlaceHolder)lv_Uc_Module.FindControl("ph_Lv_EditModule");
或
PlaceHolder ph_Lv_EditModule = (PlaceHolder)this.lv_Uc_Module.FindControl("ph_Lv_EditModule");
能否请您帮助我如何在我的 usercontrol
页面后面的代码中找到此控件.感谢您的考虑.
could you please help me how to find this control at the code behind of my usercontrol
page.
appreciate your consideration.
推荐答案
一个 ListView
通常包含多个项目,因此 NamingContainer(由 FindControl
搜索)既不是 UserControl,也不是 ListView 本身.它是 ListViewItem 对象.所以找到参考的一个地方是 ListView 的 ItemDataBound 事件一个>.
A ListView
typically contains more than one item, therefore the NamingContainer(searched by FindControl
) of your Placeholder is neither the UserControl, nor the ListView itself. It's the ListViewItem object. So one place to find the reference is the ListView's ItemDataBound event.
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var ph_Lv_EditModule = (PlaceHolder)e.Item.FindControl("ph_Lv_EditModule");
}
}
如果您需要在其他地方引用,则必须迭代 ListView 的 Items,然后在 ListViewItem
上使用 FindControl
.
If you need the reference somewhere else, you must iterate the Items of the ListView and then use FindControl
on the ListViewItem
.
顺便说一句,这与其他 DataBound 控件(如 GridView 或 Repeater)中的行为相同.
By the way, this is the same behaviour as in other DataBound Controls like GridView or Repeater.
这篇关于如何从用户控件页面后面的代码中找到 ListView 的 ItemTemplate 中的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!