Thursday, December 8, 2011

Jquery Load DropDownList form server Side DataTable Using Json

Server side code


[System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[System.Web.Services.WebMethod]


public static string GetValue()
{


System.Data.DataTable dt = new System.Data.DataTable();


dt.Columns.Add("EmpId", typeof(string));


dt.Columns.Add("EmpName", typeof(string));




dt.Rows.Add("001", "Mohan");
dt.Rows.Add("002", "Raja");
dt.Rows.Add("001", "Mohan1");
dt.Rows.Add("002", "Raja32");
dt.Rows.Add("001", "Mohan4");
dt.Rows.Add("002", "Raja6");
dt.Rows.Add("001", "Mohan7");
dt.Rows.Add("002", "Raja8");




string sJSON = JSON_DataTable(dt);


return sJSON;


}




Buliding Json Format


public static string JSON_DataTable(DataTable dt)
{


StringBuilder JsonString = new StringBuilder();


//Exception Handling
if (dt != null && dt.Rows.Count > 0)
{
JsonString.Append("{ ");
JsonString.Append("\"Head\":[ ");


for (int i = 0; i < dt.Rows.Count; i++)
{
    JsonString.Append("{ ");
    for (int j = 0; j < dt.Columns.Count; j++)
    {
      if (j < dt.Columns.Count - 1)
       {
         JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() +"\":" + "\"" +
         dt.Rows[i][j].ToString() + "\",");
       }
   else if (j == dt.Columns.Count - 1)
   {
      JsonString.Append("\"" +
     dt.Columns[j].ColumnName.ToString() + "\":" +
      "\"" + dt.Rows[i][j].ToString() + "\"");
  }
}


/*end Of String*/
if (i == dt.Rows.Count - 1)
{
   JsonString.Append("} ");
}
else
{
   JsonString.Append("}, ");
}
}


   JsonString.Append("]}");
   return JsonString.ToString();
}
else
{
  return null;
}


}


Client Side Code


$(document).ready(function () {
$('#'').mouseover(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetValue",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {


$("#DropDownList1").get(0).options.length = 0;
$("#DropDownList1").get(0).options[0] = new Option("Select", "-1");


var Result = $.parseJSON(msg.d);


$.each(Result.Head, function () {
//alert(this.EmpId + " " + this.EmpName);


$("#DropDownList1").get(0).options[$("#DropDownList1").get(0).options.length] = new Option(this.EmpName, this.EmpId);
});


},
error: function () {
alert("Failed to load");
}
})
return false;


});


});




HTML Code

Above Code:




 
Below Code:


 







Dynamically Pass controlId to method

function MyConfirmMethod(ControlName) {


var control1 = ControlName.id;


$.ajax({
type: "POST",
url: "Default.aspx/GetValue",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {


document.getElementById(control1).options.length = 0;


document.getElementById(control1).options.length = 0;
document.getElementById(control1).options[0] = new Option("Select", "-1");


var Result = $.parseJSON(msg.d);


$.each(Result.Head, function () {
//alert(this.EmpId + " " + this.EmpName);


document.getElementById(control1).options[document.getElementById(control1).options.length] = new Option(this.EmpName, this.EmpId);
});


},
error: function () {
alert("Failed to load");
}
})
return false;
}

Abstract Factory Vs Factory Method

Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes ...