[英]how to find control in ItemTemplate of the ListView from code behind of the usercontrol page?
actually, i'm developing a web template using ASP.NET and C#. 实际上,我正在使用ASP.NET和C#开发Web模板。 i have a listview
in a usercontrol
page and inside the ItemTemplate
i have a PlaceHolder
as below: 我在usercontrol
页面中有一个listview
,并且在ItemTemplate
有一个PlaceHolder
,如下所示:
<asp:PlaceHolder ID="ph_Lv_EditModule" runat="server"> </asp: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
,并且我使用了如下所示的其他方法,但是我无法访问它。
PlaceHolder ph_Lv_EditModule = (PlaceHolder)lv_Uc_Module.FindControl("ph_Lv_EditModule");
or 要么
PlaceHolder ph_Lv_EditModule = (PlaceHolder)this.lv_Uc_Module.FindControl("ph_Lv_EditModule");
could you please help me how to find this control at the code behind of my usercontrol
page. 您能帮我如何在usercontrol
页面后面的代码中找到该控件。 appreciate your consideration. 感谢您的考虑。
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. 一个ListView
通常包含多个项目,因此,占位符的NamingContainer (由FindControl
搜索)既不是UserControl,也不是ListView本身。 It's the ListViewItem object. 这是ListViewItem对象。 So one place to find the reference is the ListView's ItemDataBound event . 因此,找到引用的一个地方是ListView的ItemDataBound事件 。
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");
}
}
If you need the reference somewhere else, you must iterate the Items of the ListView and then use FindControl
on the ListViewItem
. 如果在其他地方需要引用,则必须迭代ListView的Items,然后在ListViewItem
上使用FindControl
。
By the way, this is the same behaviour as in other DataBound Controls like GridView or Repeater. 顺便说一句,这与其他DataBound控件(如GridView或Repeater)中的行为相同。
As Tim Schmelter mentioned, you can also access your control by iterating through your ListView
as follows 正如Tim Schmelter所提到的,您还可以通过遍历ListView
来访问ListView
,如下所示
private void HideMyEditModule()
{
foreach (var item in lv_Uc_Module.Items)
{
PlaceHolder holder = item.FindControl("ph_Lv_EditModule") as PlaceHolder;
if (holder!= null)
holder.Visible = false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.