[英]How to add options that i read from sql using C# web service into select element?
i have a streets table in my sql database, what i'm trying to do is to push the name of each street into a select element in html page, so my code is : 我在sql数据库中有一个street表,我想做的就是将每条街道的名称推入html页面的select元素中,所以我的代码是:
[WebMethod]
public string FillStreetsIntoSelect()
{
string streetsNames = "";
command.Connection.Close();
JavaScriptSerializer json = new JavaScriptSerializer();
command.CommandText = "select Street_Name from Streets";
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
streetsNames += reader.GetValue(0).ToString() + ",";
}
command.Connection.Close();
return json.Serialize(streetsNames);
}
and this is what this function returns: 这是此函数返回的内容:
<string xmlns="http://tempuri.org/">"asd,fgh,qwe,ert,"</string>
so this data i need to insert it into empty select element that i have in my html page by a javascript/jQuery function 所以我需要通过javascript / jQuery函数将此数据插入到我的html页面中的空选择元素中
function fillStreetLocationSelect() {
$.ajax({
async: true,
url: web + "/FillStreetLocationSelect",
dataType: "json",
method: "POST",
data: JSON.stringify(hazard),
contentType: "application/json; charset=utf-8",
success: function (data) {
/* */
}
});
}
now, how to slipt the string that the function returns, and how to insert it into the select element 现在,如何滑动函数返回的字符串,以及如何将其插入到select元素中
Find below example , may be this is very helpful to you. 查找下面的示例,可能对您很有帮助。
$.ajax({
url: "WebService/WebService.asmx/GetAllCompany",
data: "",
type: 'POST',
contentType: 'application/json;',
dataType: 'json',
success: function (result) {
if (result != null) {
var userlist = result;
var t = JSON.parse(userlist.d);
var ddl = document.getElementById('ddlCompany');
$('#ddlCompany').html('');
for (var i = 0; i < t.length; i++) {
ddl.options[i] = new Option(t[i].Name, t[i].Link);
}
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
alert(textStatus);
alert(errorThrown);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.