How to display posts from each category separately

Topics: ASP.NET 2.0, Business Logic Layer, Controls, Themes
Jan 30, 2008 at 8:38 PM
I'd like to be able to organize posts on the page by category. Specifically, i need to have 1 category called "Alerts" to always be at the top of the main page. Is this possible? I am an asp.net novice, so I really don't have a clue. :)

Thanks for any help!
Trish
Jan 30, 2008 at 9:19 PM
You would need to make a custom control that uses Post.GetPostsByCategory() and pass in the Guid of the Alerts category and then include that control into your default.aspx page, or whatever page you were using as your front page.

I should build this myself since I'm going to be doing something like this shortly but using a tab control to display the categories.
Jan 31, 2008 at 2:20 PM
Thanks for the reply ilektran. I'm very new to asp.net coding language...I've been fooling around with this and have not been able to get it right (don't know what i'm doing:). If you do create this control, would you be willing to share it with me. Below is one version of what i've tried - i guess i'm not sure how to pass the Guid - i just don't know the syntax:
-----------------------------------------------
#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using BlogEngine.Core;

#endregion

namespace Controls
{
public class UniqueCategory : Control
{
public static List<Post> GetPostsByCategory(1ab435f7-79cb-4e3c-adab-2dfb14b997d5);
}
}

-------------------------------------------
I got the number in ( ) from the categories xml file "category id". Like i said...i really don't know what i'm doing! :)

With the above code i get the errors "type expected" and "invalid token" for the "-" symbol.

Thanks for your help,
Trish




ilektran wrote:
You would need to make a custom control that uses Post.GetPostsByCategory() and pass in the Guid of the Alerts category and then include that control into your default.aspx page, or whatever page you were using as your front page.

I should build this myself since I'm going to be doing something like this shortly but using a tab control to display the categories.

Jan 31, 2008 at 4:01 PM
No problem Trish. I'll put it up here when I get it done.

The problem you are experiencing is that you are sending the Guid in a type format that it doesn't recognize. You would have to parse that Guid from a string value first such as:

Guid alertsId = new Guid("1ab435f7-79cb-4e3c-adab-2dfb14b997d5");

Then pass alertsId to the method.

To make it safer you could check to see if the Guid being used is a valid Guid and then use a try/catch in sending the Guid to a GetCategory method (Category alerts = Category.GetCategory(alertsId)) and after that pass the category by ID (Post.GetPostsByCategory(alerts.Id)).

I'm sure someone can correct me on this if I made a mistake.
Feb 1, 2008 at 3:52 PM
Thanks very much ilektran for walking me through this.

I'm getting an (invalid token "(" and ")" ) for the last line: "Post.GetPostsByCategory(alerts.Id);"

This is the code I've got:

#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using BlogEngine.Core;

#endregion

namespace Controls
{
public class UniqueCategory : Control
{
Guid alertsId = new Guid("1ab435f7-79cb-4e3c-adab-2dfb14b997d5");
Category alerts = Category.GetCategory(alertsId);
Post.GetPostsByCategory(alerts.Id);


}
}

I look forward to seeing what you come up with for this control.

Thanks very much.
Trish
Jul 9, 2008 at 4:51 PM
Didn't see anyone solve this....

So....

In /blog/User controls

make a copy of PostList.ascx

rename it PostCatList.ascx

Add this to the BindPosts() function   (replace guid with your guid (you can get it from the data table in SQL (Categories) or from the xml file in the App Data folder)

 

Guid catname = new Guid("0acaf291-698a-45e2-9589-56d2c661d427");

 

Posts =

Post.GetPostsByCategory(catname);



then on the page you want this to display...

 

<%

@ Register Src="User controls/PostCatList.ascx" TagName="PostCatList" TagPrefix="uc2" %>


and then where you want them to display

 

<

uc2:PostCatList ID="PostCatList1" runat="server" />