I need for the Comments UC to make a collection of all the latest comments bound to the repeater in, for example, List<CommentInfo>
and send it to Comment
UC through DataRef
property so I can display each comment.
I've tried to make a collection inside Comments_ItemDataBound
, but I have no way of knowing when I've been through all the data since comments.Items.Count
doesn't have a total item count before all the comments are being data bound.
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++;
}
}
The problem this question is asking is how to make a List<CommentInfo>
collection of all the comments to pass them to DataRef
property.
Comments
User Control has a repeater for showing the latest comments.
The repeater's DataSource
is from SQL Server.
<asp:Repeater ID="comments" runat="server" onitemdatabound="Comments_ItemDataBound">
<ItemTemplate>
<uc1:Comment ID="comment1" runat="server" />
</ItemTemplate>
</asp:Repeater>
Comment
UC is to show one comment from the collection of comments it receives from Comments
's repeater through public property DataRef
type of Object
which I will cast to any class I need before I send it to Comment
UC, at least that's my idea.
<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>
One way is to ask for the data as he iterate the repeater
<asp:Repeater ID="comments" runat="server">
<ItemTemplate>
<uc1:Comment ID="comment1" runat="server"
DataRef="<%#GetMyReference(Container.DataItem)%>" />
</ItemTemplate>
</asp:Repeater>
and on code behind
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");
}
Note that the DataRef
is a public value on your user control.
similar answers: Add a list of self created web user controls to a listview in codebehind fails and Virtual Listview for ASP.net?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.