我无法获得一个页面方法来向Select2控件提供客户端列表,以便可以查看,筛选和选择它们。
页面方法
获取并过滤客户端列表。 (但这种方法从未被调用过?!?)
[System.Web.Services.WebMethod]
public static string GetClients(int pageSize, int page, string searchingFor)
{
try
{
var gcDAL = new AllClear.DAL.GloryClientDAL();
var clients = gcDAL.GetClients();
var filteredClients = from m in clients where m.ClientName.StartsWith(searchingFor)
select new { id = m.ClientMasterCode, text = m.ClientName };
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(filteredClients);
}
catch (Exception ex)
{
throw;
}
}
的jQuery
使用Igor Vaynberg的Select2方法:http://ivaynberg.github.io/select2/
$(document).ready(function () {
$("#e11").select2({
placeholder: "Select a client",
minimumInputLength: 1,
allowClear: true,
id: function (results) {
return {
id: results.id
};
},
ajax: {
url: "Tinkerer.aspx/GetClients", // ?searchTerm=" + (term=null)?"":term, // current page's method. GetClients, GetCountries
quietMillis: 150,
dataType: 'json',
type: "POST",
contentType: "application/json; charset=utf-8",
data: function (term, page) {
return {
pageSize: 100,
pageNum: page,
searchingFor: term
};
},
results: function (data, page) {
var more = (page * pageSize) < data.Total;
return {
results: data,
more: more
};
}
},
formatResult: ClientFormatResult,
formatSelection: ClientFormatSelection,
dropdownCssClass: "bigdrop",
escapeMarkup: function (m) {
return m;
}
});
})
function ClientFormatResult(clients) {
var markup = "<table><tr><td>clients.text</td></tr></table>";
return markup;
}
function ClientFormatSelection(clients) {
return clients.text; // ClientName
}
目标字段
目标字段必须是input type="hidden"
。
<p>
<input type="hidden" name="e11" id="e11" style="width: 300px" class="populate" />
</p>