Creating A List Of Unique Tags For Your Site Part 1
For this blog, I created a database field that has a comma delimited list of tags for that particular blog posting.
The idea is that when you click on that particular tag, you will see a list of all articles with that matching tag.
For example, the tag field in the database for this article might be "ASP.net, Awesomeness", which would display those
two tags as clickable links.
I'm not going to go into how to create those links or how to save the comma delimited fields for this example, that
should be simple enough to figure out. What I am going to do is create a listing of all the tags in the database which
users will be able to use as a sort of navigation device to find blog posts matching these tags. The end product of this example will look a whole lot like the picture at the top right of this post.
To display the listing, we're just going to create label and populate it with a alphabetical list of links. We could
use a ListBox object, but that is beyond the scope of this article. Our basic label will look like:
<asp:Label ID="lblTags" runat="server" />
Now in the code behind we will create a two functions, LoadTags() and AddTags(). LoadTags() will create the listing
and set the text property of our label. AddTags() will be responsible for seperating our comma delimated database
values, checking for duplicate tags, and adding them to an array list.
Make a call to LoadTags() in the Page_Load event. Since this listing doesn't need to be refreshed (and we don't like
unnecessary database reads), setup this call so it runs only on the initial page load.
protected void Page_Load(object sender, EventArgs e)
Also create an arrayList named that is globally accessible. Certainly it doesn't need to be global (nor should it
probably be) but it makes this example easier to follow and you can change that in your own code if you so desire.
private ArrayList blogTagArray = new ArrayList();
You will also need to include System.Data.SqlClient for this example to work, so add the following line to the top of your code behind page:
LoadTags() is the meat and potatoes of this example. In it we will loop through all the rows in the database and add
their respective tags to an ArrayList (done via the AddTags() function). After we've gone through everything, we will
sort the ArrayList and then create the string we'll use for the label. To spice things up a little bit, we're also
going to use a random number to change the font size of each tag link.
protected void LoadTags()
string sqlString = "SELECT BlogTags FROM FakeBlogTable ORDER BY BlogTags ASC";
SqlConnection MyConnection = new SqlConnection("XXX");
SqlCommand objCmd = new SqlCommand(sqlString, MyConnection);
MyReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
This article has been view 462 times.