问题描述
ajax webmethod is not working while dropdown bind
我尝试过:
< select class =filter_selectid =sr_picker>
< / select>
< script type =text / javascript>
google .setOnLoadCallback(loadsr);
函数loadsr(){
// alert();
$ .ajax({
url:/ webmethod.aspx / Getsr,
dataType:json,
类型:POST ,
contentType:application / json; charset = utf-8,
成功:函数(r){
var ddlsection = $ ([id * = sr_picker]);
// alert(ddlsection);
ddlsection.empty()。append('< option selected =selected value =0>所有< /选项> ');
$ .each(rd,function(){
ddlsection.append($(< option>< / option>)。val(这个['价值'])。html(这['文字']));
});
},
错误: function(XMLHttpRequest,textStatus,errorThrown){
alert('得到错误');
}
});
}
< / script>
公共静态列表< listitem> Getsr()
{
MasterLogic objMas = new MasterLogic();
// Utilities objUtl = new Utilities();
列表< listitem> sectionname = new List< listitem>();
string qry =select sales_person_code,sales_personnel from sales_personnel;
DataTable dt = objMas.GetDataTable(qry);
if(dt == null)
返回sectionname;
List<数据行> list = dt.AsEnumerable()。ToList();
for(int j = 0; j< dt.Rows.Count; j ++)
{
sectionname.Add(new ListItem
{
Value = list [j] .ItemArray [0] .ToString(),
Text = list [j] .ItemArray [1] .ToString()
});
}
返回sectionname;
}
What I have tried:
<select class="filter_select" id="sr_picker" ">
</select>
<script type="text/javascript">
google.setOnLoadCallback(loadsr);
function loadsr() {
// alert();
$.ajax({
url: "/webmethod.aspx/Getsr",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function(r) {
var ddlsection = $("[id*=sr_picker]");
//alert(ddlsection);
ddlsection.empty().append('<option selected="selected" value="0">All</option>');
$.each(r.d, function() {
ddlsection.append($("<option></option>").val(this['Value']).html(this['Text']));
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('Got an Error ');
}
});
}
</script>
public static List<listitem> Getsr()
{
MasterLogic objMas = new MasterLogic();
// Utilities objUtl = new Utilities();
List<listitem> sectionname = new List<listitem>();
string qry = "select sales_person_code,sales_person_name from sales_personnel";
DataTable dt = objMas.GetDataTable(qry);
if (dt == null)
return sectionname;
List<datarow> list = dt.AsEnumerable().ToList();
for (int j = 0; j < dt.Rows.Count; j++)
{
sectionname.Add(new ListItem
{
Value = list[j].ItemArray[0].ToString(),
Text = list[j].ItemArray[1].ToString()
});
}
return sectionname;
}
推荐答案
这篇关于下拉绑定时,Ajax webmethod无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!