Search code examples
c#asp.nettagsasp.net-ajaxtagging

Tag System in Asp.net


I want to create a tagging system for my website which allows user to enter the required skills,separated by comma, using ASP.net and C#.

In detail:

  • A textbox will receive tags, separated by comma.
  • Suggestions will be provided while typing, based on AVAILABLE tags in my database.
  • Suggested tags will be displayed, below the textbox.
  • If a new tag is encountered, it is inserted into database.

The tags (separated by comma), given by the user could be further manipulated according to my needs (a way of doing that).

I want to make a separate entry for each and every tag into the database.

I tried using Tag-it by Levy Carneiro Jr.. it is working perfect for local source.

But when I tried attaching it with my database using this. It just doesn't work.

My code:-

<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "tag.aspx/GetAutoCompleteData",
                    data: "{'username':'" + document.getElementById('singleFieldTags2').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            }
        });
    }

<script>
    $(function () {
  //Local sample-      //var sampleTags = ['c++', 'java', 'php', 'coldfusion', 'javascript', 'asp', 'ruby', 'python', 'c', 'scala', 'groovy', 'haskell', 'perl', 'erlang', 'apl', 'cobol', 'go', 'lua'];

        $('#singleFieldTags2').tagit({

        });
    });
    </script>

<body>
<form id="form1" runat="server">

    <asp:TextBox name="tags" id="singleFieldTags2" value="Apple, Orange" class="autosuggest" runat="server"></asp:TextBox>


</form>

Backend C# code-

[WebMethod]

public static List<string> GetAutoCompleteData(string username)
{
    List<string> result = new List<string>();
    using (SqlConnection con = new SqlConnection("Data Source=ZESTER-PC;Initial Catalog=mystp;Integrated Security=True"))
    {
        using (SqlCommand cmd = new SqlCommand("select tag_name from tags where tag_name LIKE '%'+@SearchText+'%'", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText", username);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(dr["tag_name"].ToString());
            }
            return result;
        }
    }
}

Here tags is my tag table containing tag_id and tag_name.


Solution

  • I have created the Tagging System using ASP.net

    Check it out.. nd do rate it..

    Tagging System using ASP.net by Sumanyu Soniwal