将对象传递给动态加载的ListView ItemTemplate [英] Pass object to dynamically loaded ListView ItemTemplate
问题描述
我一直在关注以下有关在ListView控件中使用多个ItemTemplates的帖子.
I have been following the following post on using multiple ItemTemplates in a ListView control.
尽管在此示例中确实产生了输出,但我正在尝试找出如何将一个对象通过psas传递到ItemTemplate的用户控件,而我似乎无法执行/设计该对象.
While following this example does produce output, I am trying ot figure out how to psas an object through to the ItemTemplate's user control, which I do not seem to be able to do/figure out.
protected void lvwComments_OnItemCreated(object sender, ListViewItemEventArgs e)
{
ListViewDataItem currentItem = (e.Item as ListViewDataItem);
Comment comment = (Comment)currentItem.DataItem;
if (comment == null)
return;
string controlPath = string.Empty;
switch (comment.Type)
{
case CommentType.User:
controlPath = "~/layouts/controls/General Comment.ascx";
break;
case CommentType.Official:
controlPath = "~/layouts/controls/Official Comment.ascx";
break;
}
lvwComments.ItemTemplate = LoadTemplate(Controlpath);
}
用户控件如下:
public partial class OfficialComment : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
在此示例中,值正在ascx页中输出:
In the example, the values are being output in the ascx page:
<%# Eval("ItemName") %>
但是我需要访问此控件中的ListItem来执行其他逻辑.我无法弄清楚如何通过我的评论项目发送.发件人对象和EventArgs不包含该信息.
however I need to access the ListItem in this control to do other logic. I cannot figure out how I to send through my Comment item. The sender object and EventArgs do not contain the info.
理想情况下,我想获得有关使用<%#Eval%>语句时控件如何访问数据项的说明.我能够确定的是以下几种方式访问当前项目的方法:
Ideally, I would like to obtain an explanation as to how the control accesses the dataitem when using the <%# Eval %> statement. What I have been able to determine, is the following way to gain access to the current item:
我创建了一个自定义ListView控件,该控件在ItemCreating上设置了dataItemIndex.
I have created a custom ListView control, which set's the dataItemIndex on ItemCreating.
在我的官方评论控件中,添加以下内容:
In my Official comment control, I add the following:
List<Comment> commentList = ((CommentListView)this.Parent.Parent.Parent).DataSource as List<Comment>;
if (commentList != null)
{
int currentIndex = ((ListViewDataItem)this.Parent).DataItemIndex;
Comment currentItem = commentList[currentIndex];
}
推荐答案
虽然有关于动态项目模板的文档,但是除了<%#Eval%>函数之外,没有其他示例可以访问数据.在尝试了一些问题后,我不喜欢必须递归遍历控制树.
While there is documentation on dynamic Item Templates, no examples progressed through to accessing data other that the <%# Eval %> function. After trying some methods, shown in my question, I was not a fan of having to recursively traverse up the control tree.
我能够做的是创建一个从UserControl继承的类.此类将定义我的DataItem:
What I've been able to do is create a class that inherits from UserControl. This class will define my DataItem:
public partial class ListViewTemplateControl<T> : UserControl where T : class
{
public T CurrentItem { get; set; }
}
然后,在我的ListView中,我可以执行以下操作:
Then, in my ListView, I can perform the following:
protected void lvwComments_OnItemCreated(object sender, ListViewItemEventArgs e)
{
ListViewDataItem currentItem = (e.Item as ListViewDataItem);
Comment comment = (Comment)currentItem.DataItem;
if (comment == null)
return;
string controlPath = string.Empty;
switch (comment.Type)
{
case CommentType.User:
controlPath = "~/layouts/controls/General Comment.ascx";
break;
case CommentType.Official:
controlPath = "~/layouts/controls/Official Comment.ascx";
break;
}
ListViewTemplateControl<Comment> templateControl = LoadControl(controlPath) as ListViewTemplateControl<Comment>;
if (templateControl != null)
{
templateControl.CurrentItem = comment;
templateControl.ID = comment.ItemID;
lvwComments.Controls.Add(templateControl);
}
}
现在,在模板控件中,我可以基于传递的CurrentItem(DataItem)执行所有自定义逻辑.唯一需要注意的是<%#Eval%>函数现在将无法使用.
In my template control, I can now perform all my custom logic based on the CurrentItem (DataItem) that I pass through. The only caveat to this is that <%# Eval %> function will not work now.
这篇关于将对象传递给动态加载的ListView ItemTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!