例如,我需要评论UC收集绑定到转发器的所有最新评论,List<CommentInfo>
然后Comment
通过DataRef
属性将其发送给UC,以便显示每个评论。
我试图在内部创建一个集合Comments_ItemDataBound
,但是我无法知道何时查看所有数据,因为comments.Items.Count
在绑定所有注释之前没有总项目数。
int count = 1;
List<CommentInfo> commentsList = new List<CommentInfo>();
protected void comments_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
cil.Add(new CommentInfo
{
username = DataBinder.Eval(e.Item.DataItem, "username").ToString()
...
});
// comments.Items.Count here is 0, on next data bound will be 1 and count will be 2
if (comments.Items.Count == 5)
{
var comment = e.Item.FindControl("comment1") as Comment;
comment.DataRef = commentsList;
}
count++;
}
}
这个问题要问的问题是如何List<CommentInfo>
收集所有注释以将它们传递给DataRef
属性。
Comments
用户控件具有用于显示最新评论的转发器。
中继器DataSource
来自SQL Server。
<asp:Repeater ID="comments" runat="server" onitemdatabound="Comments_ItemDataBound">
<ItemTemplate>
<uc1:Comment ID="comment1" runat="server" />
</ItemTemplate>
</asp:Repeater>
Comment
UC将Comments
通过公共属性DataRef
类型显示从中继器收到的评论集合中的一个评论,在Object
将其发送给Comment
UC之前,我将其强制转换为所需的任何类,至少这是我的想法。
<div class="comment" id="<%= DataRef.comment_id %>">
<a class="comment-link" href="/<%= DataRef.friendly_url %>">
Written by: <strong><%= DataRef.username %></strong>, <%= DataRef.comment_date %></em>
<%= DataRef.comment_text %></a>
</div>
一种方法是在迭代中继器时询问数据
<asp:Repeater ID="comments" runat="server">
<ItemTemplate>
<uc1:Comment ID="comment1" runat="server"
DataRef="<%#GetMyReference(Container.DataItem)%>" />
</ItemTemplate>
</asp:Repeater>
以及后面的代码
public object GetMyReference(object oItem)
{
// I am not sure what you wish to return here...
// or you may want to full return the oItem, I am not sure
// this is an example
return DataBinder.Eval(oItem, "yourDataItem");
}
请注意,DataRef
是用户控件上的公共值。
类似的答案:将自己创建的Web用户控件的列表添加到codebehind中的listview后失败,并且ASP.net的Virtual Listview是否成功?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句