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

Forums / Developing with Sitefinity / Create own functions in Forum lists?

Create own functions in Forum lists?

7 posts, 2 answered
  1. Magnus
    Magnus avatar
    28 posts
    Registered:
    26 May 2009
    09 Dec 2009
    Link to this post
    Hi there. (again..)

    Working atm with a community for a new customer. This community contains different groups (with members, roles, forums etc, etc). User can create new groups themself and that automatically creates a forum, guestbook, roles, priviliges etc.

    It's almost done... But i need to get one more thing to work out to reach the finishline.


    When i'm at a groups startspage. i want to list information from that certain group. That includes the forum for that group. But the forum-control is showing all groups-forum in the list. So... i want to edit:

    protected void ForumsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        { 
                ForumManager manager = new ForumManager(); 
                GridView inner = (GridView)item.FindControl("inner"); 
                IList posts = manager.GetForums(((Telerik.Forums.ICategory)item.DataItem).ID, true); 
     
                if (!UserManager.IsCurrentUserInRole(((Telerik.Forums.ICategory)item.DataItem).Name)) 
                    item.Visible = false
                inner.DataSource = posts
                inner.DataBind(); 
     

    that function. But when i try to debug that file i really cant get into the code-behind. So... to my questions:

    1. Is it possible to get this code to run? (when i'm editing the .aspx file i can see changes, but when i try to debug in VS the .ascx file i cant catch the process). Not even i Page_Load.

    2. Can you something like this in that code

    if(forum.title == "some title"
       //show that forum in the list 
    else 
       // dont show that forum 

    I'm familiar with only showing one forum using a GUID in the Url.
    ex:   start.aspx?mode=singleForum&forum=6f9dba14-4f51-41b7-ae4c-ab89fbedfd1e

    But that is just a backup plan. Cause that mega-url will need to be on every startpage for every group. Which isnt so beauty.



    Well.. hope anyone can understand my problem and maybe give me an helping hand.

    Kind Regards / Magnus.

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    09 Dec 2009
    Link to this post
    Hi Magnus,

    Most probably the template has not been converted correctly and the events are not fired. Make sure that AutoEventWireup attribute exists in your template.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Magnus
    Magnus avatar
    28 posts
    Registered:
    26 May 2009
    09 Dec 2009
    Link to this post
    Thanks for that quick answer.

    The template i'm using is the standard  Sitefinity/ControlTemplates/Forums/ForumList.ascx for Sitefinity 3.7 SP1.(did just see that you have released a SP2 which i've missed.. ;)

    Tried with adding:

    <%@ Control AutoEventWireup="true" %> 
     // and even
    <%@ Control AutoEventWireup="false" %> 
    // didnt know which one i was needed. 


    But with no luck.

    Any other suggestions?




  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    09 Dec 2009
    Link to this post
    Hello Magnus,

    You can bind to the event handlers for each control there is on the template from code. Please see sample code:

    <%@ Control Language="C#" %>
    <%@ Register Assembly="Telerik.Security" Namespace="Telerik.Security.WebControls" TagPrefix="prof" %>
    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %>
     
    <script runat ="server" type="text/C#">
        protected void Page_Load(object sender, EventArgs e)
        {
            //subscribe to row data bound event of uncategorized grid
            this.Uncategorized.RowDataBound += new GridViewRowEventHandler(Uncategorized_RowDataBound);
             
            //subscribe to repeater databound so you can get the grid holding forums for each template
            this.forumsRepeater.ItemDataBound += new RepeaterItemEventHandler(forumsRepeater_ItemDataBound);
        }
        //event handler for forum categories repeater
        void forumsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //find forum grid and subscribe to row data bound
            GridView categoryGrid = (GridView) e.Item.FindControl("inner");
            if (categoryGrid != null)
                categoryGrid.RowDataBound += new GridViewRowEventHandler(categoryGrid_RowDataBound);
             
        }
        //event handler for grid holding categorized forums
        void categoryGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //add same logic as in the first sample code I sent
        }
        //event handler for grid holding uncategorized forums
        void Uncategorized_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
                // get the current node of sitemap
                Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode;
                // get the CmsPage represented by the current node and extract its ID
                Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage;
                Telerik.Forums.IForum forum = e.Row.DataItem as Telerik.Forums.IForum;
                if (currentPage.Name == "Forums" && forum.Name=="Test Forum 2")
                {
                    e.Row.Visible = false;
                }else if (currentPage.Name == "Forums" && forum.Name == "Test Forum 2")
                {
                    e.Row.Visible = false;
                }            
            }
        }
    </script>
     
    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %>
     
    Test Test
    <sfWeb:CssFileLink ID="CssFileLink1" FileName="~/Sitefinity/ControlTemplates/Forums/forumsCommonLayout.css" Media="screen" runat="server" />
     
    <div class="sf_forumsWrapper">
    <asp:Repeater ID="forumsRepeater" runat="server">
        <HeaderTemplate>
             
        </HeaderTemplate>
        <ItemTemplate>
     
                <h2 class="sf_forumCategoryTitle"><%#DataBinder.Eval(Container.DataItem, "Name")%></h2>
     
            <asp:GridView ID="inner" runat="server" CssClass="sf_forumsList" AutoGenerateColumns="false" GridLines="none">
                <Columns>
                    <asp:TemplateField HeaderText="<%$Resources:Status %>">
                        <ItemTemplate>
                            <asp:Literal ID="forumStatus" runat="server"></asp:Literal>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="<%$Resources:Forum %>">
                        <ItemTemplate>
                            <asp:LinkButton ID="btnOpenForum" runat="server"></asp:LinkButton>
                            <div><asp:Literal id="lblDescription" runat="server"></asp:Literal></div>
                        </ItemTemplate>
                        <ItemStyle CssClass="sf_forumTitle" />
                    </asp:TemplateField>
                     
                    <asp:BoundField DataField="PostsCount" HeaderText="<%$Resources:Posts %>" />
                    <asp:BoundField DataField="ThreadsCount" HeaderText="<%$Resources:Threads %>" />
                    <asp:TemplateField HeaderText="<%$Resources:LastPost %>" HeaderStyle-CssClass="sf_forumAuthor">
                        <ItemTemplate>
                            <asp:LinkButton ID="goToPost" runat="server" CausesValidation="false"><sfWeb:DateTimeLiteral ID="lastPostDate" runat="server" DateFormatString="dd MMM yyyy, hh:mm"></sfWeb:DateTimeLiteral></asp:LinkButton>
                            <asp:Label ID="noPosts" runat="server" Text="<%$Resources:NoPosts %>"></asp:Label>
           
                            <asp:Label ID="byText" runat="server" Text="By "></asp:Label>
                            <prof:ManageProfile ID="lastPostAuthor" runat="server" ShowAnonymousLabel="true" AnonymousLabel="<%$ Resources:Anonymous %>">
                                <ViewTemplate>
                                    <asp:Literal ID="FirstName" runat="Server"></asp:Literal
                                    <asp:Literal ID="LastName" runat="Server"></asp:Literal>
                                </ViewTemplate>
                            </prof:ManageProfile>
                        </ItemTemplate>
                        <ItemStyle CssClass="sf_forumAuthor" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ItemTemplate>
        <FooterTemplate>
             
        </FooterTemplate>
    </asp:Repeater>
     
            <h2 class="sf_forumCategoryTitle"><asp:Literal ID="UncategorizedHeader" runat="server" Text="<%$Resources:Uncategorized %>"></asp:Literal></h2>
     
        <asp:GridView ID="Uncategorized" runat="server" CssClass="sf_forumsList"  AutoGenerateColumns="false" GridLines="none">
            <Columns>
                <asp:TemplateField HeaderText="<%$Resources:Status %>">
                    <ItemTemplate>
                        <asp:Literal ID="forumStatus" runat="server"></asp:Literal>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="<%$Resources:Forum %>">
                    <ItemTemplate>
                        <asp:LinkButton ID="btnOpenForum" runat="server"></asp:LinkButton>
                        <div><asp:Label ID="lblDescription" runat="server"></asp:Label></div>
                    </ItemTemplate>
                    <ItemStyle CssClass="sf_forumTitle" />
                </asp:TemplateField>
                 
                <asp:BoundField DataField="PostsCount" HeaderText="<%$Resources:Posts %>" />
                <asp:BoundField DataField="ThreadsCount" HeaderText="<%$Resources:Threads %>" />
                <asp:TemplateField HeaderText="<%$Resources:LastPost %>" HeaderStyle-CssClass="sf_forumAuthor">
                    <ItemStyle CssClass="sf_forumAuthor" />
                    <ItemTemplate>
                        <asp:LinkButton ID="goToPost" runat="server" CausesValidation="false"><sfWeb:DateTimeLiteral ID="lastPostDate" runat="server" DateFormatString="dd MMM yyyy, hh:mm"></sfWeb:DateTimeLiteral></asp:LinkButton>
                        <asp:Label ID="noPosts" runat="server" Text="<%$Resources:NoPosts %>"></asp:Label>
                      
                        <asp:Label ID="byText" runat="server" Text="By "></asp:Label>
                        <prof:ManageProfile ID="lastPostAuthor" ShowAnonymousLabel="true" AnonymousLabel="<%$ Resources:Anonymous %>" runat="server">
                            <ViewTemplate>
                                <asp:Literal ID="FirstName" runat="Server"></asp:Literal
                                <asp:Literal ID="LastName" runat="Server"></asp:Literal>
                            </ViewTemplate>
                        </prof:ManageProfile>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>

    This sample code removes forums from the forums list based on some criteria.


    Kind regards,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
  5. Magnus
    Magnus avatar
    28 posts
    Registered:
    26 May 2009
    09 Dec 2009
    Link to this post
    Damn i love your support.

    Ok. i got it to work. Did some changes, cause i have all my forums in a certain category.
    using this:

    <%@ Control Language="C#" AutoEventWireup="true" %> 
    <%@ Register Assembly="Telerik.Security" Namespace="Telerik.Security.WebControls" TagPrefix="prof" %> 
    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %> 
     
    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %> 
     
     
    <script runat ="server" type="text/C#"
        protected void Page_Load(object sender, EventArgs e) 
        { 
              
            //subscribe to repeater databound so you can get the grid holding forums for each template 
            this.forumsRepeater.ItemDataBound += new RepeaterItemEventHandler(forumsRepeater_ItemDataBound); 
        } 
        //event handler for forum categories repeater 
        void forumsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        { 
            //find forum grid and subscribe to row data bound 
            GridView categoryGrid = (GridView) e.Item.FindControl("inner"); 
            if (categoryGrid != null) 
                categoryGrid.RowDataBound += new GridViewRowEventHandler(categoryGrid_RowDataBound); 
        } 
        //event handler for grid holding categorized forums 
        void categoryGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
            if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
                Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager(); 
                // get the current node of sitemap 
                Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode; 
                // get the CmsPage represented by the current node and extract its ID 
                Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage; 
                Telerik.Forums.IForum forum = e.Row.DataItem as Telerik.Forums.IForum; 
                if (forum.Name !="Forum1") 
                { 
                    e.Row.Visible = false
                } 
            } 
        } 
    </script> 
     
     
     
    <sfWeb:CssFileLink ID="CssFileLink1" FileName="~/Sitefinity/ControlTemplates/Forums/forumsCommonLayout.css" Media="screen" runat="server" /> 
    ... 
    ... 
    ... 
    ... 


    But... If i want to have that code in code-behind. What shall i do then to get that code to run?


    Thanks so very much for the example btw.

    Regards / Magnus



  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    09 Dec 2009
    Link to this post
    Hi Magnus,

    You can create a code file for the template and separate the code. To do this, please follow these steps:

    1) Create the code file and a public partial class where you will put the methods. Lets say you create this file in the same directory as the control template and name it ForumList.ascx.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.WebControls;
     
    /// <summary>
    /// Summary description for ForumList
    /// </summary>
    public partial class ForumList : System.Web.UI.UserControl
    {
        //put code here
    }

    Then modify the control template like this:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/Sitefinity/ControlTemplates/Forums/ForumList.ascx.cs" Inherits="ForumList" %>

    This should do the trick.

    Regards,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
  7. Magnus
    Magnus avatar
    28 posts
    Registered:
    26 May 2009
    10 Dec 2009
    Link to this post
    - "Knock knock.. who's there?"
    - "My brain?"

    Ofc i forgot to add:
    CodeFile="~/Sitefinity/ControlTemplates/Forums/ForumList.ascx.cs" Inherits="ForumList" 

    Edit: Btw, that file is already there from Sitefinity. That's why I was kinda suspicious when i couldn't debug the .cs file.



    Thanks a lot Radoslav / Ivan.



Register for webinar
7 posts, 2 answered