using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
namespace jQueryDialog
{
/// <summary>
/// Summary description for JSONP1
/// </summary>
public class JSONP1 : IHttpHandler
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string callBackFunction = string.Empty;
string jsonpRequestData = string.Empty;
string jsonpResponse = string.Empty;
int maxRows = 100;
public void ProcessRequest(HttpContext context)
{
if (context.Request.Params.Get(1) != null) //.Params.Get(1)
callBackFunction = context.Request.Params.Get(0).ToString();
if (context.Request.Params.Get(1) != null)
jsonpRequestData = context.Request.Params.Get(1).ToString();
CompaniesQuery cq = new CompaniesQuery();
cq = jsSerializer.Deserialize<CompaniesQuery>(jsonpRequestData);
if (cq.maxRows != null)
maxRows = (int)cq.maxRows;
using (jQueryDialogDataContext db = new jQueryDialogDataContext())
{
var q = (from c in db.ezpl8_Companies
orderby c.Company
where c.Company.Contains(cq.term) || (cq.term == null)
select new Company { value = c.CoID, label = c.Company }).Take(maxRows).ToList();
jsonpResponse = jsSerializer.Serialize(q);
}
string strOutput = string.Format("{0}({1});", context.Request["callback"], jsonpResponse);
context.Response.AddHeader("Content-Length", strOutput.Length.ToString());
context.Response.ContentType = "application/json";
context.Response.Write(strOutput);
}
public bool IsReusable
{
get
{
return false;
}
}
}
public class Company
{
int _value;
string _label;
public int value
{
get { return _value; }
set { _value = value; }
}
public string label
{
get { return _label; }
set { _label = value; }
}
}
public class CompaniesQuery
{
int? _maxRows;
string _term;
public int? maxRows
{
get { return _maxRows; }
set { _maxRows = value; }
}
public string term
{
get { return _term; }
set { _term = value; }
}
}
}