Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Creating A List Of Unique Tags For Your Site Part 1
Date 6/3/2008    Tags C#, ASP.net    (0)

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)
    {

        if (!IsPostBack)
        {
            LoadTags();
        }

    }

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:
using System.Data.SqlClient;

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);

        objCmd.Connection.Open();

        SqlDataReader MyReader;

        MyReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

        while (MyReader.Read())
        {
            AddTags((string)MyReader["BlogTags"]);

                    
                    


This article has been view 462 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)