5
Answers

Bind a dropdownlist inside ajax

This is the code to bind dropdownlist inside ajax success using the dataset returned from webmethod...
JavaScript function
function Check(ddldesig)
{
alert('hi');
alert(ddldesig);
//var selectedText = $(ddldesig).text();
var selectedText=ddldesig.options[ddldesig.selectedIndex].text;
var selectedValue = $(ddldesig).val();
alert("Selected Text: " + selectedText + " Value: " + selectedValue);
var obj = {};
obj.desigId = selectedValue;
obj.desig = selectedText;
$.ajax({
type: "POST",
url: "JobAssign.aspx/FillEmp",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response){
alert(response.d);
var ddlEmp = $("[id*=ddlEmp]");
var dtset = jQuery.parseJSON(response.d);
var dtEmp = dtset["Employee"];
ddlEmp.empty().append('<option selected="selected" value="-1">Select</option>');
$.each(dtEmp, function () {

ddlEmp.append($("<option></option>").val(this['EmpId']).html(this['FirstName']));
});
//});
},
failure: function (response){
alert("failed"+response.d);
}
});
}
Webmethod
public static string FillEmp(int desigId, string desig)
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
try
{
objEmpBL = new EmployeeBL();
objEmpBL.EmpId = 0;
objEmpBL.EmpCode = "";
objEmpBL.CompId = 0;
objEmpBL.CatId = 0;
objEmpBL.DeptId = 0;
objEmpBL.ActDesgId = desigId;
objEmpBL.VisaIssue = "";
objEmpBL.VisaExpiry = "";
dt = objEmpBL.EmpReportSelect();
dt.TableName = "Employee";
ds.Tables.Add(dt);
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
return ds.GetXml();
}
In the above code at the javascript function I got the correct datatable as response..but the extraction fails..any help....

Answers (5)