This is a migrated thread and some comments may be shown as answers.

Nested repeater

1 Answer 94 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Spire
Top achievements
Rank 1
Spire asked on 10 Jul 2012, 12:32 PM
I'm having some problem getting a repeater with a nested repeater update with ajaxmanger.

Se example page here: http://dev12.spireweb.no/repeater_ajax.aspx 

I just want the nested repeater in the post to update when a comment this added or deleted. This only works for the first post.
Is there a way to solved this issue?

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="btnRefreshPosts" EventName="onclick">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="pnlPosts" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="repPosts" EventName="onitemcommand">
                    <UpdatedControls>                   
                        <telerik:AjaxUpdatedControl ControlID="pnlComments" />                 
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="repComments" EventName="onitemcommand">
                    <UpdatedControls>                   
                        <telerik:AjaxUpdatedControl ControlID="pnlComments" />                 
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>


<asp:Panel ID="pnlPosts" runat="server">
                   <div id="Posts" style="padding:10px;margin:15px; width:700px;">
                       <asp:Repeater ID="repPosts" runat="server" onitemdatabound="repPosts_ItemDataBound" OnItemCommand="repPosts_ItemCommand">
                           <ItemTemplate>
                               <div class="postitem" >
                                   <div class="sep"></div>
                                   <div class="postitem_name" >
                                       <asp:Literal ID="litName" runat="server" />
                                       <asp:Literal ID="litCreated" runat="server" />                                   
                                   </div>
                                   <div class="postitem_content">
                                       <asp:Button ID="btnDelete" runat="server" Text="" CssClass="btnDelete" />
                                       <asp:Literal ID="litContent" runat="server" />
                                   </div>                                   
                               </div>
                               <div style="clear:both;"></div>
                                   <asp:Panel ID="pnlComments" runat="server">
                                   <div class="CommentsWrapper">
                                       <asp:Repeater ID="repComments" runat="server" OnItemDataBound="repComments_ItemDataBound" OnItemCommand="repComments_ItemCommand">
                                           <HeaderTemplate>
                                                   <h3>Comments</h3>
                                           </HeaderTemplate>
                                           <ItemTemplate>
                                                   <div class="Comment">       
                                                       <asp:Button ID="btnDelete" runat="server" Text="" CssClass="btnDelete" />                                       
                                                       <asp:Literal ID="litComment" runat="server" />                                               
                                                   </div>
                                           </ItemTemplate>
                                       </asp:Repeater>
                                       <div class="AddComment">
                                           <asp:Panel ID="pnlAddComment" runat="server" DefaultButton="btnAddComment">
                                               <asp:TextBox ID="txtComment" runat="server"></asp:TextBox>
                                               <asp:Button ID="btnAddComment" CssClass="common btnAddComment" runat="server" Text="Submit" />
                                           </asp:Panel>
                                       </div>
                                       <div style="clear:both;"></div>
                                   </div>
                               </asp:Panel>
                           </ItemTemplate>                   
                       </asp:Repeater>
                   </div>
               </asp:Panel>


protected void GetPosts()
    {
        repPosts.DataSource = WineclubManager.GetPosts(19,20);
        repPosts.DataBind();
    }
    protected void repPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            PostItem row = (PostItem)(e.Item.DataItem);
            CustomerItem customer = CustomerManager.GetCustomer(row.CustomerID);
            Repeater repComments = (Repeater)e.Item.FindControl("repComments");
 
            Literal litName = (Literal)e.Item.FindControl("litName");
            Literal litCreated = (Literal)e.Item.FindControl("litCreated");
            Literal litContent = (Literal)e.Item.FindControl("litContent");
            TextBox txtComment = (TextBox)e.Item.FindControl("txtComment");
 
            Button btnAddComment = (Button)e.Item.FindControl("btnAddComment");
            Button btnDelete = (Button)e.Item.FindControl("btnDelete");
 
            if (row.CustomerID != 0)
                litName.Text = "<span>" + customer.Firstname + " " + customer.Lastname + "</span>";
            else
                litName.Text = "<span>LabelWines</span>";
 
            litCreated.Text = "<span class=\"date\">" + row.Created.ToString("dd.MM.yyyy HH:mm") + "</span>";
            litContent.Text = row.Content;
 
            txtComment.Text = "Write a comment";
 
            if (row.CustomerID == CustomerManager.GetCustomerID())
            {
                btnDelete.CommandName = "Delete";
                btnDelete.CommandArgument = row.ID.ToString();
                btnDelete.Attributes.Add("onclick", "jConfirm('" + Resources.Language.Wineclub_PostPopupMessage.Replace(Environment.NewLine, "<br />") + "', '" + Resources.Language.Wineclub_PostPopupHeader + "', function(r) { if(r==true) __doPostBack(\"" + btnDelete.ClientID.Replace("_", "$") + "\", \"\"); }); return false;");
            }
            else
                btnDelete.Visible = false;
 
            btnAddComment.CommandName = "AddComment";
            btnAddComment.CommandArgument = row.ID.ToString();
            BindInnerRepeaterComments(repComments, row.ID);
        }
    }
    protected void repPosts_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "AddComment":
                CommentItem item = new CommentItem();
                item.PostID = int.Parse(e.CommandArgument.ToString());
                item.CustomerID = 2;
 
                TextBox txtComment = e.Item.FindControl("txtComment") as TextBox;
                if ((txtComment.Text.Length > 0) && (txtComment.Text != "Write a comment"))
                    item.Comment = txtComment.Text;
                int id = WineclubManager.AddComment(item);
 
                txtComment.Text = Resources.Language.Wineclub_PostDefaultCommentText;
                Repeater repComments = (Repeater)e.Item.FindControl("repComments");
                BindInnerRepeaterComments(repComments, int.Parse(e.CommandArgument.ToString()));
 
                RadAjaxManager ra = (RadAjaxManager)(Page.FindControl("RadAjaxManager1"));
                ra.ResponseScripts.Add("javascript:InitCommentTextFields();");
                 
                break;
        }
        switch (e.CommandName)
        {
            case "Delete":
                WineclubManager.DeletePost(int.Parse(e.CommandArgument.ToString()));
                GetPosts();
 
                RadAjaxManager ra = (RadAjaxManager)(Page.FindControl("RadAjaxManager1"));
                ra.ResponseScripts.Add("javascript:InitCommentTextFields();");
                ra.ResponseScripts.Add("javascript:YoutubePosts();");
                break;
        }
    }
    private void BindInnerRepeaterComments(Repeater Rep, int PostID)
    {
        CommentCollection items = WineclubManager.GetComments(PostID, false);
        if (items.Count > 0)
        {
            Rep.DataSource = items;
            Rep.DataBind();
            Rep.Visible = true;
        }
        else
            Rep.Visible = false;
    }
    protected void repComments_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            CommentItem row = (CommentItem)e.Item.DataItem;
            CustomerItem customer = CustomerManager.GetCustomer(row.CustomerID);
 
            Literal litComment = (Literal)e.Item.FindControl("litComment");
            Button btnDelete = (Button)e.Item.FindControl("btnDelete");
 
            litComment.Text = "<span class=\"commentname\">" + customer.Firstname + " " + customer.Lastname + " - </span><span class=\"commentdate\">" + row.Created.ToString("dd.MM.yyyy HH:mm") + "</span>" + "<span class=\"commentmessage\">" + row.Comment + "</span>";
 
            if (row.CustomerID == 2)
            {
                btnDelete.CommandName = "Delete";
                btnDelete.CommandArgument = row.ID.ToString();
                btnDelete.Attributes.Add("onclick", "jConfirm('" + Resources.Language.Wineclub_PostCommentPopupMessage.Replace(Environment.NewLine, "<br />") + "', '" + Resources.Language.Wineclub_PostCommentPopupHeader + "', function(r) { if(r==true) __doPostBack(\"" + btnDelete.ClientID.Replace("_", "$") + "\", \"\"); }); return false;");
            }
            else
                btnDelete.Visible = false;
 
        }
    }
    protected void repComments_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        CommentItem currentComment = WineclubManager.GetComment(int.Parse(e.CommandArgument.ToString()));
        Repeater currentRep = (Repeater)source;
        switch (e.CommandName)
        {
            case "Delete":
                WineclubManager.DeleteComment(int.Parse(e.CommandArgument.ToString()));
                //GetPosts();
 
                BindInnerRepeaterComments(currentRep, currentComment.PostID);
 
                RadAjaxManager ra = (RadAjaxManager)(Page.FindControl("RadAjaxManager1"));
                ra.ResponseScripts.Add("javascript:InitCommentTextFields();");
 
                break;
        }
    }

1 Answer, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 13 Jul 2012, 11:14 AM
Hi Kari,

Could you please try to set ClientIDMode for the page where the repeater controls is placed to "AutoID"  and verify if this makes any difference?

Regards,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Ajax
Asked by
Spire
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Share this question
or