.cs代码在这里
[WebMethod]
public DataTable GetName()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("CodeNo", typeof(string));
dt.Rows.Add("Delhi","D01");
dt.Rows.Add("Noida", "N01");
return dt;
}
代码
$(document).ready(function () {
$().ready(function () {
$.ajax({
type: "POST",
url: "Home.aspx/GetName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#City").get(0).options.length = 0;
$("#City").get(0).options[0] = new Option("--Select--", "-1");
?.......Here ....................?
});
},
error: function () {
alert("Failed to load ");
}
});
});
我的外出
- - 仅选择 - ,但我想要... SelectList或DropDpwnlist中的DataTable Row(“Delhi”,“Noida”) 像
-Select--
Delhi
Noida
答案 0 :(得分:0)
.cs代码
[WebMethod]
public string GetName()
{
string Result = string.Empty;
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("CodeNo", typeof(string));
dt.Rows.Add("Delhi", "D01");
dt.Rows.Add("Noida", "N01");
if (dt.Rows.Count != 0)
{
foreach (DataRow Dr in dt.Rows)
{
Result = string.Concat(Result + "<option value=" + Dr["CodeNo"] + ">" + Dr["Name"] + "</option>");
}
}
return Result;
}
.aspx代码
$(document).ready(function () {
$().ready(function () {
$.ajax({
type: "POST",
url: "Home.aspx/GetName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#City").html(msg.d)
});
},
error: function () {
alert("Failed to load ");
}
});
});
答案 1 :(得分:0)
您的后端代码看起来很好。我看到你正在使用JSON来填充下拉列表。看看下面的代码。我经常使用它。
$.getJSON("Home.aspx/GetName", function (data) {
var dropDown = $("#City");
dropDown.append($("<option></option>").val('').text('- Select -'));
$.each(data, function () {
dropDown.append($("<option></option>")
.val($(this).attr("CodeNo"))
.text($(this).attr("Name")));
});
});