1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / BlogPost Details View Custom Template Post Back Data Loss

BlogPost Details View Custom Template Post Back Data Loss

2 posts, 0 answered
  1. Nidhi
    Nidhi avatar
    40 posts
    Registered:
    20 Jul 2010
    23 Nov 2011
    Link to this post
    Hi,
    I have created a custom template for the blog post details view.
    Within the custom template I have two custom links for paging to the next post and previous post.
    When I submit the comment form, if the comment data is invalid, my custom links become blank again.
    After searching for solutions, I tried setting EnableViewState="true" but that didnt make any difference.
    Can anyone tell me what I am doing wrong?
    Below id the code for my custom template:
    <%@ Control Language="C#" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Comments" Assembly="Telerik.Sitefinity" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
     
    <%@ Import Namespace="Telerik.Sitefinity" %>   
    <%@ Import Namespace="Telerik.Sitefinity.Modules.Blogs" %>
    <%@ Import Namespace="Telerik.Sitefinity.GenericContent.Model" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Linq" %>
     
     
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SingleItemContainer.ItemDataBound += new EventHandler<RadListViewItemEventArgs>(SingleItemContainer_ItemDataBound);
        }
        void SingleItemContainer_ItemDataBound(object sender, RadListViewItemEventArgs e)
        {
            if (e.Item is RadListViewDataItem)
            {
                 
                RadListViewDataItem item = e.Item as RadListViewDataItem;
                Telerik.Sitefinity.Blogs.Model.BlogPost post = item.DataItem as Telerik.Sitefinity.Blogs.Model.BlogPost;
                if (post != null)
                {
                    //FIND CONTROLS
                    HyperLink hl_comments = e.Item.FindControl("hl_comments") as HyperLink;
                    HyperLink hl_prev = e.Item.FindControl("hl_prev") as HyperLink;
                    HyperLink hl_next = e.Item.FindControl("hl_next") as HyperLink;
                     
                    BlogsManager manager = BlogsManager.GetManager();
                    var query = manager.GetComments().Where<Comment>(c => c.CommentedItemID == post.Id &&
                        c.CommentStatus == CommentStatus.Published);
                     
                    //SHOW COMMMENTS LINKS / COUNT
                    hl_comments.NavigateUrl = Request.Url.ToString() + "#comments";
                    if (query.Count() == 0)
                    {
                        hl_comments.Text = "Go Comment!";
                        hl_comments.Enabled = false;
                    }
                    else if (query.Count() == 1)
                        hl_comments.Text = query.Count().ToString() + " Comment";
                    else
                        hl_comments.Text = query.Count().ToString() + " Comments";
                     
                   
                     
                    // SHOW PREVIOUS NEXT LINKS
                    IQueryable<Telerik.Sitefinity.Blogs.Model.BlogPost> live_posts = manager.GetBlogPosts().Where(b => b.Parent == post.Parent && b.Status == ContentLifecycleStatus.Live);
                    string current_node = "http://" + Request.Url.Host + "/" + Telerik.Sitefinity.Web.SiteMapBase.GetActualCurrentNode().UrlName;
                     
                    try {
                        var prev_post = live_posts.Where(t => t.PublicationDate < post.PublicationDate).OrderByDescending(t => t.PublicationDate).First();
     
                        if (prev_post != null) {
                            hl_prev.NavigateUrl = current_node + prev_post.ItemDefaultUrl;
                        }else{
                            hl_prev.Visible = false;  
                        }
                    } catch (Exception ex) {
                        //sequence contains no elements
                        hl_prev.Visible = false;
                    }
     
                    try {
                        var next_post = live_posts.Where(n => n.PublicationDate > post.PublicationDate).OrderBy(n => n.PublicationDate).First();
     
                        if (next_post != null)
                        {
                            hl_next.NavigateUrl = current_node + next_post.ItemDefaultUrl;
                        }
                        else
                        {
                            hl_next.Visible = false;
                        }
                    } catch (Exception ex2) {
                        //sequence contains no elements
                        hl_next.Visible = false;
                    }
     
                 
                }
            }
        }
    </script>
     
     
    <sf:ResourceLinks ID="resourceLinks" runat="server">
        <sf:ResourceFile Name="CSS/blogs.css" />
    </sf:ResourceLinks>
     
    <telerik:RadListView ID="SingleItemContainer" ItemPlaceholderID="ItemContainer" AllowPaging="False" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false">
        <LayoutTemplate>
            <div class="sfpostDetails">
                <%--<div class="sfpostLinksWrp">
                    <sf:MasterViewHyperLink ID="MasterViewHyperLink1" class="sfpostBack" Text="<%$ Resources:BlogResources, allposts %>" runat="server" />
                </div>--%>
                <asp:PlaceHolder ID="ItemContainer" runat="server" />
            </div>
        </LayoutTemplate>
        <ItemTemplate>
        <div class="pubDate">
            <sf:FieldListView ID="PostDate" runat="server" Format="{PublicationDate.ToLocal():MMMM dd, yyyy}" />
             
        </div>
         
            <sf:FieldListView ID="PostTitle" runat="server"
                Text="{0}" Properties="Title"
                WrapperTagName="h1" WrapperTagCssClass="sfpostTitle"
            />
            <div class="author">
                <asp:Literal ID="Literal2" Text="<%$ Resources:Labels, By %>" runat="server" />
                <sf:PersonProfileView ID="PersonProfileView1" runat="server" />
            </div>
     
            <sf:ContentBrowseAndEditToolbar ID="BrowseAndEditToolbar" runat="server" Mode="Edit,Delete,Unpublish"></sf:ContentBrowseAndEditToolbar>
            <sf:FieldListView ID="PostContent" runat="server"
                Text="{0}" Properties="Content"
                WrapperTagName="div" WrapperTagCssClass="sfpostContent"
            />
     
            <span class="labels">LABELS:</span> <sitefinity:FlatTaxonField ID="FlatFieldControl" DisplayMode="Read" runat="server" WebServiceUrl="~/Sitefinity/Services/Taxonomies/FlatTaxon.svc" AllowMultipleSelection="true" TaxonomyId="CB0F3A19-A211-48a7-88EC-77495C0F5374" TaxonomyMetafieldName="Tags" Expanded="false" ExpandText="ClickToAddTags" BindOnServer="true" CssClass="tagList_wrapper" />
            <br clear="all" />
     
             
            <asp:HyperLink ID="hl_comments" runat="server" CssClass="sfpostCommentsCount" />
            <sf:DetailsViewHyperLink ID="FullStory" Text="Permanent link to this post" runat="server" CssClass="sfpostFullStory" />
     
            <div id="postnavigation">
                <div class="prevpost">
                    <asp:hyperlink runat="server" id="hl_prev" Text="Previous Post" />
                </div>
                <div class="nextpost">
                    <asp:hyperlink runat="server" id="hl_next" Text="Next Post" />
                </div>
            </div>
            <div id="postacomment"><p>Comments</p></div>
            <sf:ContentView
                 id="commentsListView"
                 ControlDefinitionName="BlogsCommentsFrontend"
                 DetailViewName="CommentsMasterView"
                 ContentViewDisplayMode="Master"
                 LayoutTemplatePath="~/CustomTemplates/CommentsMasterView.ascx"
                 runat="server"
                  />
            <sf:ContentView
                 id="commentsDetailsView"
                 ControlDefinitionName="BlogsCommentsFrontend"
                 DetailViewName="CommentsDetailsView"
                 ContentViewDisplayMode="Detail"
                 LayoutTemplatePath="~/CustomTemplates/CommentsDetailsView.ascx"
                 runat="server"
                  />
        </ItemTemplate>
    </telerik:RadListView>

    thank you so much
    Nidhi
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Sep 2017
    28 Nov 2011
    Link to this post
    Hi Nidhi,

    Thank you for contacting us.

    Excuse me I was unable to properly view the output of the code. The template was not rendering properly. I suppose you are using Sitefinity 4.1 for using the incline script block to write scripts. Please note inline scripts are no longer supported after Sitefinity 4.2 and eventual upgrade will make the template return "object reference not set to the instance of the object" error.

    I suppose the problem is coming from the paging links not bound to the proper RadListView. With blog post there are two views DetailsPostView (DetailsView) and MasterPostView (PostList), the RadListView that lists blog details or blogs list is different for both views.
    In this case DetailsPostView the RadListView items are accessed trough DetailsView.
    protected virtual RadListView DetailsView
            {
                get
                {
                    return this.Container.GetControl<RadListView>("SingleItemContainer", true);
                }
            }
    Use DetailsView_ItemDataBound
    namespace SitefinityWebApp.DetailView
    {
        public class Test : Telerik.Sitefinity.Modules.Blogs.Web.UI.Public.DetailPostsView
        {
      
            protected override void InitializeListView(IQueryable<Telerik.Sitefinity.Blogs.Model.BlogPost> query, int? totalCount)
            {
                base.InitializeListView(query, totalCount);
      
                this.DetailsView.ItemDataBound += new EventHandler<RadListViewItemEventArgs>(DetailsView_ItemDataBound);
                this.DetailsView.PreRender += new EventHandler(DetailsView_PreRender);
            }
      
            void DetailsView_PreRender(object sender, EventArgs e)
            {
                //throw new NotImplementedException();
            }
      
            void DetailsView_ItemDataBound(object sender, RadListViewItemEventArgs e)
            {
                if (e.Item is RadListViewDataItem)
                {
                    var rep = e.Item.FindControl("ImageButton1") as Repeater;
     
                            }
                        }
                    }
                }
            }


    In case you need to use external code file here is a post where it is described how this can be achieved (I suggest making a test on a separate project).
    You will need a template .ascx with build action ->Embedded Resource. and a code file inheriting from Telerik.Sitefinity.Modules.Blogs.Web.UI.Public.DetailPostView and in it specify the template used. This is done the same way as with custom controls.
    public override string LayoutTemplatePath
            {
                get
                {
                    return ClassName.layoutTemplate;
                }
                
            }
    private const string layoutTemplate = "~/VppPrefix/SitefinityWebApp.DetailsView.BlogPostFrontend.ascx";
    The embedded template is accessed trough a virtual path with a specific name "~/VppPrefix(put name here)" To register the virtual path go to
    Administration->Settings->Advanced->VirtualPathSettings->VirtualPaths and create new virtual path
    VirtualPath: ~/NameOfThePath/*
    ResourceLocation: Name of your assembly
    ResolverName: EmbeddedResourceResolver
    Restart the application in order for settings to take effect.

    To register the newly used template with custom code go to Administration->Settings->Advanced->Blogs->Controls->BlogPostsFrontend->Views->DetailsBlogPostsFrontend and in field ViewType enter Namespace.class of your custom template.

    Greetings,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered