如何找到code中的用户控件的页面背后在ListView的ItemTemplate的控制? [英] how to find control in ItemTemplate of the ListView from code behind of the usercontrol page?
问题描述
其实,我正在开发使用ASP.NET和C#中的网页模板。
我有一个列表视图
在用户控件
页,里面的的ItemTemplate
我有一个占位符
如下:
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>
我想访问该占位符
从code的后面,我有如下用不同的方法,但我不能访问它。
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");
您可以请帮我如何找到我的用户控件
页面背后的code这种控制。
AP preciate你考虑。
could you please help me how to find this control at the code behind of my usercontrol
page.
appreciate your consideration.
推荐答案
A 的ListView
通常包含多个项目,因此的 NamingContainer (通过搜索的FindControl
)的占位符既不是用户控件,也不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中的项目,然后用的FindControl
在 ListViewItem的
。
If you need the reference somewhere else, you must iterate the Items of the ListView and then use FindControl
on the ListViewItem
.
顺便说一句,这是相同的行为在其他数据绑定控件像GridView控件或Repeater。
By the way, this is the same behaviour as in other DataBound Controls like GridView or Repeater.
这篇关于如何找到code中的用户控件的页面背后在ListView的ItemTemplate的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!