how to find control in ItemTemplate of the ListView from code behind of the usercontrol page?(如何从用户控件页面后面的代码中找到 ListView 的 ItemTemplate 中的控件?)
问题描述
实际上,我正在使用 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 中的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从用户控件页面后面的代码中找到 ListView 的 ItemTemplate 中的控件?


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01