我使用的是 html 下拉菜单,而不是 asp .net 控件。我想使用 jquery 在下拉列表中填充数据库中的员工姓名。我必须在 aspx.cs 文件中编写什么代码。以及如何绑定 json 数据?请回复
My code is
<script type="text/javascript" language="javascript">
$(document).ready(function()
{
$().ready(function(){
$.ajax({
type:"POST",
data:"{}",
url:"TestJson.aspx/GetName",
dataType:"json",
success:function(msg)
{
var dropDown=$("#ddlName");
dropDown.append($("<option></option>").val('').text('- Select -'));
$.each(data, function () {
dropDown.append($("<option></option>")
.text($(this).attr("Name")));
});
},
error:function()
{
alert("failed to load");
}
});
});
</script>
<style type="text/css">
#ddlName
{
width: 146px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="border:1px solid gray; width:400px;">
Name
<select id="ddlName">
<option value="">--Select--</option>
</select>
</div>
</form>
My .cs code
public static Details[] BindDropdown()
{
DataTable dt = new DataTable();
List<Details> details = new List<Details>();
SqlConnection con = new SqlConnection("Data Source=ST0022;Initial Catalog=QuickMove_Globe;User ID=sa;Password=good");
SqlCommand cmd = new SqlCommand("SELECT ClientBranchID,Name FROM ProspectiveLead", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dtrow in dt.Rows)
{
Details detail = new Details();
detail.Id = Convert.ToInt32(dtrow["ClientBranchId"].ToString());
detail.Name = dtrow["Name"].ToString();
details.Add(detail);
}
return details.ToArray();
}
public class Details
{
public int Id { get; set; }
public string Name { get; set; }
}