简体   繁体   English

如何从用户控件页面后面的代码在ListView的ItemTemplate中找到控件?

[英]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.

相关问题 从背后的代码在Web用户控件上的Listview ItemTemplate中填充Gridview - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind 如何从后面的代码填充Repeater ItemTemplate? - How to Fill Repeater ItemTemplate from Code Behind? 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 如何在ListView中更改用作ItemTemplate的UserControl的VisualState - How to change VisualState of UserControl used as ItemTemplate in ListView 如何从同一用户控件中找到用户控件中的控件 - how to find control in usercontrol from same usercontrol 当ItemTemplate是用户控件时,如何从ListView中正确删除项目? - How to properly remove Items from a ListView when the ItemTemplate is a User Control? 如何从后面的代码中填充anRepeater.ItemTemplate - How to fill in anRepeater.ItemTemplate from code behind 如何从代码的后面访问User样式中的Setter的ControlTemplate中定义的Control实例? - How to access from code-behind an instance of a Control defined in a ControlTemplate in a Setter in a Style in an UserControl? 如何从后面的代码中找到嵌套在listView中的FormView - How to find a FormView nested in a listView from code behind 在后面的代码中找到Listview中的空数据? - Find Empty Data in Listview from code behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM