Monday 15 August 2011

Bind Dropdown using Jquery

In this article you will learn how to bind dropdown list using jquery. Suppose you want to show all employees name with their employee Id in dropdown list, then use this code.



//Function to call web method for getting Names

function GetName() {
$.ajax({
type: "POST",
url: "Emp_Name.aspx/GetEmpName",
data:"" ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onGetData_Received,
error: onGetDataError
});

}

function onGetDataError(xmlHttpObj) {

var error = eval("(" + xmlHttpObj.responseText + ")");
alert(error.Message);
}

function onGetData_Received(data) {
data = data.d;
$("#EmployeeName").append($("").val("0").html("----Select----"));

for (var i = 0; i < data.length; i++) {
$("#EmployeeName").append($("").val(data[i].employeeID).html(data[i].employeeName));

 }
}


//Web method to get list of employees names 
[WebMethod]

public static List GetEmpName()
{

BussName obj = new BussName ();
return obj.GetNames();
}

No comments:

Post a Comment