Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Adding a custom user control to show latest posts from specific forums

This articles shows how to use the existing ViewTopicsBasic control provided with InstantForum within a custom user control to display the latest posts with a specific forum and any child forums. As we are adding this to a custom user control once created the custom user control can be easily included within any page within InstantForum.

Creating The Custom User Control

1. Navigate to your InstantForum Skin folder within Windows Explorer. For example Skins/{YourSkinName}. Create a new folder called CustomControls. So this would be Skins/{YoutrSkinName}/CustomControls

2. Add a new user control called "LatestForumPosts.ascx" within the CustomControls folder.

The code you should add is shown below...

LatestForumPosts.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LatestForumPosts.ascx.cs" Inherits="Skins_InstantASP_CustomControls_LatestForumPosts" %>
<%@ Register tagPrefix="InstantASP" Namespace="InstantASP.Common.UI.WebControls" Assembly="InstantASP.Common.UI" %>
<%@ Register tagPrefix="InstantForum" Namespace="InstantASP.InstantForum.UI.Controls" Assembly="InstantASP.InstantForum.UI" %>
<%@ Register tagPrefix="InstantForumWebControls" Namespace="InstantASP.InstantForum.UI.WebControls" Assembly="InstantASP.InstantForum.UI" %>

<InstantForum:ViewTopicsBasic ID="ctlPosts" ListStyle="Grid" ItemRowCssClass="cell-light" UserCardSector="3" PageSize="20" runat="server">
<SearchEventArgs EnableRecordCount="false" SortBy="LastPosterDate" MaxResults="20" />
</InstantForum:ViewTopicsBasic>

LatestForumPosts.ascx.cs

using System;

public partial class Skins_InstantASP_CustomControls_LatestForumPosts : System.Web.UI.UserControl
{

// the forum ID we want to display latest topics for
public int ForumID;

protected void Page_Load(object sender, EventArgs e)
{

// get current forum and all child forums
InstantASP.InstantForum.Business.Forums f = new InstantASP.InstantForum.Business.Forums(InstantASP.InstantForum.HttpContext.ForumContext.Current.CurrentUser.UserID);
InstantASP.InstantForum.Collections.ForumCollection forums = f.RecurseForwards(ForumID);
InstantASP.InstantForum.Components.Forum forum = f.SelectForum(ForumID);

// shows hwo to set title
// where CustomHeader_String is defined in Resources.xml
ctlPosts.Text = "CustomHeader_String";
ctlPosts.SubHeaderText = "CustomSubHeader_String";

if (forums != null)
{

if (forums.Count > 0)
{
// pass forum IDs to the search parameters for our ViewTopicsBasic control
ctlPosts.SearchEventArgs.ForumIDs = InstantASP.Common.Utils.Array.StringToIntArray(
forums.IdentitiesAsString(false, ','), ","); ;
}

}

}
}

Adding the User Control To A InstantForum page

Now you've created the user control it's time to choose where to display this within your InstantForum page. In this example we'll choose the forum homepage. To add the user control to the forum homepage we'll need to follow the steps below...

1. Open Skins/{YourSkinName}/Default.ascx within Visual Studio or NotePad.

2. Register the custom user control at the top of the Default.ascx skin control as shown below...

<%@ Register Src="CustomControls/LatestForumPosts.ascx" TagPrefix="uc1" TagName="LatestForumPosts" %>

3. Add the mark-up to display the user control in an appropriate place within Default.ascx mark-up. For example...

<div class="row" id="pnlMain" runat="Server">     

<uc1:LatestForumPosts runat="server" ForumID="9" />

You'll notice we set the ForumID property on our custom user control which will be used to populate the ViewTopicsBasic control within the user control with topics from the forum with that ID and all child forums of the forum.