I have a tagit auto-complete jquery which runs fine when searching case numbers. I'm trying to append some case numbers already inserted in the text box from code behind and set as individual tags for case number. It does set as a tag, but I'm unable to make individual tags as it combines all case numbers to a single tag. Below is my code.
class.cs
List<Models.Case> caseRefer = CaseCon.ViewById(SelectedCase.strId); // here I'm fetching case numbers from database with ',' for separation.
List<string> casenumbers = new List<string>();
if (caseRefer[0].ReferCases!=null)
{
if (caseRefer[0].ReferCases != null)
{
casenumbers = caseRefer[0].ReferCases.Split(',').ToList(); // here I'm spiting case numbers
}
foreach (var item in casenumbers)
{
if (!item.Contains("<br />"))
{
txt_referCases.Text += item + Environment.NewLine; // here I'm trying to make all case numbers an individual tags but new line is not making any difference.
}
}
}
.aspx
<asp:TextBox ID="txt_referCases" runat="server"></asp:TextBox>
<script>
$(function () {
var arr = [];
var arrName = [];
$('#<%=txt_referCases.ClientID %>').tagit({
autocomplete: {
delay: 0,
minLength: 3,
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Case.aspx/ReferCases") %>',
data: "{ 'caseNum': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
arr = $.map(data.d, function (el) { return el });
response($.map(data.d, function (item, index) {
return {
label: item,
val: index
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
}
});
});
</script>
Finally solved my own problem. It was just a matter of replacing Environment.NewLine to ',' would do the job. So the line would be
txt_referCases.Text += item + ","
and this will separate the tags.