Hi lingers,
The Library you are using doesn't support beforeTagAdded event.
Please use below Tagit Library and refer below sample.
<link rel="stylesheet" type="text/css" href="http://aehlke.github.io/tag-it/css/jquery.tagit.css" />
<script type="text/javascript" src="https://rawgit.com/aehlke/tag-it/master/js/tag-it.js"></script>
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://aehlke.github.io/tag-it/css/jquery.tagit.css" />
<script type="text/javascript" src="https://rawgit.com/aehlke/tag-it/master/js/tag-it.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
url: "<%=ResolveUrl("Default.aspx/GetCustomers") %>",
data: "{}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
var sampleTags = [];
$.each(data.d, function (i, item) {
sampleTags.push(item);
});
$('.myTags').tagit({
availableTags: sampleTags,
beforeTagAdded: function (event, ui) {
if ($.inArray(ui.tagLabel, sampleTags) == -1) {
return false;
}
},
afterTagAdded: function (event, ui) {
var e = $.Event("keydown", { keyCode: 32 });
$(".ui-autocomplete-input").eq(0).trigger(e);
}
});
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
$(document).on('click', '[id*=btnGet]', function () {
var ids = [];
$.each($(this).closest('tr').find('.tagit-choice'), function () {
ids.push($(this).find('input[type=hidden]').val());
});
$(this).closest('tr').find('[id*=hfIds]').val(ids.join(','));
});
});
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Name" ItemStyle-Width="100" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<ul id="myTags" class="myTags">
</ul>
<asp:HiddenField ID="hfIds" runat="server" />
<asp:Button ID="btnGet" Text="Get Values" runat="server" OnClick="GetId" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
[WebMethod]
public static string[] GetCustomers()
{
List<string> customers = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT [CustomerID] FROM [Customers]";
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(sdr["CustomerID"].ToString());
}
}
conn.Close();
}
}
return customers.ToArray();
}
protected void GetId(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
string selectedTags = (row.FindControl("hfIds") as HiddenField).Value;
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + selectedTags + "')", true);
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 5 * FROM Customers";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
Screenshot