Showing posts with label tagging. Show all posts
Showing posts with label tagging. Show all posts

Saturday, March 24, 2012

Tagging in ASP.NET 2.0 - can anyone help, please?

Hello,

I have found lots of articles explaining how to build tag clouds in ASP.NET, they are very good. I have been looking for a very simple tutorial on how to allow users to tag a database entry like a photo or news article, or anything really, like on this forum - where you add tags for a new post. I can't find any. I'm quite new to this, and I would be really grateful if somone could explain how I can build a page where users can tag stuff, using a single textbox and commas as a separator for each word. Thats the bit which confuses me the most!

Thank you very much!

Jack.

Hi Jack,

There's a few steps involved, so I'll try to keep it short...

First, add a "Tags" table with the following fields to the database that will hold all of your tags:

Tags
- tagID (Integer)(Primary Key)(Identity)
- tagText (varchar(255))

Next, let's assume you already have a table that stores your Articles, and it has a primary key field named: "articleID". You would then create another table to tie multiple tags to an article:

ArticleTags
- articleID (Integer)(Not Null)
- tagID (Integer)(Not Null)

Now, without getting too far into a discussion about good architecture - let's keep things simple. Let's create classes that define our Article, Tag and ArticleTag tables:

public class Article
{
public int articleID;
public string name;
public string body;

public Article(string name, string body)
{
this.name = name;
this.body = body;
}

public void Insert()
{
//code to save an article goes here
//once article is saved, set the generated articleID into this object
this.articleID = ArticleManager.Insert(this);
}
}

public class Tag
{
public int tagID;
public string tagText;

public Tag(string tag)
{
this.tagText = tag;
}

public void Insert()
{
//first, see if tag already exists
Tag tag = TagManager.GetByTagText(this.tagText);
if (tag == null) //assumes that above method returns null if matching tag not found
{
//once tag is saved, set the generated tagID into this object
this.tagID = TagManager.Insert(this);
}
else
{
//tag exists, grab the tagID
this.tagID = tag.tagID;
}
}
}

public class ArticleTag
{
public int articleID;
public int tagID;

public ArticleTag(int articleID, int tagID)
{
this.articleID = articleID;
this.tagID = tagID;
}

public void Insert()
{
ArticleTagManager.Insert(this);
}
}

Let's also create a helper class that holds multiple "Tag" objects:

public class TagList : List<Tag>
{
public TagList() { }
}

OK, that's the backend stuff done. Now all we need to do is get the tags from the user, parse them out and use these classes to save them to the database. Let's assume that we're on a page where we are creating a new Article and Tags for that article at the same time:

After the "Save" button is clicked, so the code goes here:

protected void btnSave_Click(object sender, EventArgs e)
{
//build up the new Article object
Article art = new Article(txtName.Text, txtBody.Text);

//now, let's grab the tags which are seperated by commas
TagList tList = new TagList();
string[] tags = txtTags.Text.Split(',');
//loop through the resulting array
foreach (string t in tags)
{
//first, make sure there's more than just a space
if (t.Trim().Length > 0)
{
Tag tag = new Tag(t.Trim());
//add tag to the list
tList.Add(tag);
}
}
this.mNewTagList = tList;

//now, save article and tags
art.Insert();

//make sure it saved and we have an articleID to use
if (art.articleID > 0)
{
//now save each tag, and create association between the tag and the article
foreach(Tag t in tList)
{
t.Insert();

//make sure we have a tagID to work with
if (t.tagID > 0)
{
//create a new association
ArticleTag at = new ArticleTag(art.articleID, t.tagID);
at.Insert();
}
}
}
}

That's it! Hope that helps!

Chad

Tagging in .NET

I'm wondering if anyone could help me in figuring out the architecture / database tables needed to implement tagging into my website. Users would be tagging their RSS feeds.

Are there any good articles that explain the architecture and tables necessart to implement this?

Thanks in advance.could you explain what you mean by "tagging"?

"implement tagging into my website"

"tagging their RSS feeds."

Whats this "tagging" ?


lol.. jinx Curt... i had my reply window open and stepped away for a bit before i finished my post
Thanks for your reply...

I thought that "tagging" is a fairly well know functionality in the internet world today. I guess I am wrong. Please let me explain...

Flickr.com - allows you to associate words to a photo, or...tag the photo with a descriptive word
http://del.icio.us/ - allows you to "tag" a link with a descriptive word or words.

In my case, I have RSS feeds that I would like to allow the users to "tag" or to associate a descriptive word or words to a feed.

Here is a great article that explains it in more detail. What Im looking to learn and implement is the architecture and datbase tables necessary to incorporate this into my website.

http://freshtakes.typepad.com/znetlady/2005/07/tagging_what_is.html

Thanks again.

So in short:

You are asking "why am I making this more complex than adding a simple 'description' column to a database table" ?

I seriously don't see all the confusing and over-thinking of adding one extra field onto your data table(s) for your users to add/edit/search-on