Send data to a user control inside another user control

What I need

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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x