Hello Everyone.
Good day! I would like to seek some assistance on how to retain the newly created value in selectize. My code below worked fine. But one thing i noticed, everytime I moved to another control the value disappear. I want to know if i miss something in my code. Hope you can provide some guidance. Thank you.
$pmeds = $('#MainContent_admitmedicines').selectize({
loadingClass: 'selectizeLoading',
valueField: 'medID',
labelField: 'medicineName',
searchField: 'medicineName',
options: [],
delimiter: ',',
persist: false,
maxItems: 10,
create: function (input, callback) {
if (!input.length) return callback();
var id=0;
$.ajax({
url: 'DataService.asmx/addMedicines',
type: 'get',
dataType: 'json',
data: {
'profileID': docID,
'med': input
},
error: function () {
callback();
},
success: function (res) {
if (res) {
return callback({
value: res[0].medID,
text: res[0].medicineName
});
}
}
});
},
load: function (query, callback) {
if (!query.length) return callback();
$.ajax({
url: 'DataService.asmx/getMedicines',
method: 'post',
dataType: 'json',
data: {
profileID: docID
},
error: function () {
callback();
},
success: function (res) {
callback(res);
}
});
},
onChange: function (value) {
medsID.value = value;
}
});
#region medicine
[WebMethod]
public void getMedicines(int profileID) {
string constr = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
List<Medicines> medicines = new List<Medicines>();
using (SqlConnection con = new SqlConnection(constr)) {
con.Open();
using (SqlCommand cmd = new SqlCommand("spGetMedicines", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@profileID", SqlDbType.VarChar).Value = profileID;
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) {
Medicines medicine = new Medicines();
medicine.medID = Convert.ToInt32(rdr["medID"]);
medicine.medicineName = rdr["medicineName"].ToString();
medicine.profileID = Convert.ToInt32(rdr["profileID"]);
medicines.Add(medicine);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(medicines));
}
}
[WebMethod]
public void addMedicines(int profileID, string med) {
string constr = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
List<Medicines> medicines = new List<Medicines>();
using (SqlConnection con = new SqlConnection(constr)) {
con.Open();
try {
using (SqlCommand cmd = new SqlCommand("spInsertMedicine", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@profileID", profileID);
cmd.Parameters.AddWithValue("@medicine", med);
object count = cmd.ExecuteScalar();
System.Diagnostics.Trace.WriteLine(count.GetType());
int newId = Convert.ToInt32(count);
Medicines medicine = new Medicines();
medicine.medID = newId;
medicine.medicineName = med;
medicine.profileID = profileID;
medicines.Add(medicine);
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(medicines));
}
catch (Exception ex) {
}
}
}
#endregion
<div id="admitmeds" runat="server" class="form-group">
<label for="exampleInputText">Medicines:</label>
<asp:DropDownList ID="admitmedicines" runat="server" CssClass="demo-default"></asp:DropDownList>
</div>