我有一个用户控件进入C#。在用户控件中有一个下拉列表,我正在使用select2。将数据填充到下拉列表我正在调用Web方法。但是当我运行代码时,web方法没有被调用。 以下是我的代码:
SearchPinCode.ascx
<select class="form-control select2" id="pinCode">
</select>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#pinCode").select2({
ajax: {
type: "POST",
url: '<%= ResolveUrl("SearchPinCode.ascx/GetPinCode") %>',
dataType: 'json',
delay: 250,
async: true,
contentType: "application/json; charset=utf-8",
data: function (params) {
return JSON.stringify({
prefixText: params
});
},
results: function (data) {
console.log(data);
return { results: data };
},
cache: true,
tags: true
},
minimumInputLength: 1
});
});
网络方法:
[System.Web.Services.WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetPinCode(string prefixText)
{
string Query = string.Empty, ErrorMsg = string.Empty;
Query = "....";
List<string> items = new List<string>(10);
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter(Query, conn);
da.Fill(ds);
}
return JsonConvert.SerializeObject(ds.Tables[0], Formatting.Indented);
}