kwfreelife
|
 |
« on: March 23, 2015, 07:42:41 PM » |
|
Hi I use ComboBox with Custom format, it cannot get data from the web service. Below is my code. ----ASMX------ public class DataServices : System.Web.Services.WebService {
[WebMethod] public string HelloWorld() { List<XData> data = new List<XData>(); data.Add(new XData { Id = 1, TextName = "nammmme", Desc = "ddd" }); data.Add(new XData { Id = 2, TextName = "dddd", Desc = "dffffdd" }); // We are using an anonymous object above, but we could use a typed one too (SayHello class is defined below) // SayHello data = new SayHello { Greeting = "Hello", Name = firstName + " " + lastName };
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(data); } }//end class
public class XData { public int Id { get; set; } public String TextName { get; set; } public String Desc { get; set; } } ----HTML---------- $('#cc3').combobox({ url:DataServices.asmx/HelloWorld, valueField: 'Id', textField: 'TextName', panelWidth: 650, panelHeight: 'auto', formatter: function (row) { var s = "<table><tr><td width='50px'><b>" + row.TextName + "</b></td><td width='600px'>" + row.Desc + "</td></tr></table>" return s; } }); <input id="cc3" class="easyui-combobox" name="c3" value="" /> ------ The solution above that I use, it doesn't display the data in the ComboBox although there are the data that return from the web service. However if I use code below it can display. ---Solution 1: use the ASHX file. ------- public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; List<XData> data = new List<XData>(); data.Add(new XData { Id = 1, TextName = "komkk", Desc = "ddd" }); data.Add(new XData { Id = 2, TextName = "dddd", Desc = "dffffdd" }); // We are using an anonymous object above, but we could use a typed one too (SayHello class is defined below) // SayHello data = new SayHello { Greeting = "Hello", Name = firstName + " " + lastName };
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
context.Response.Write( js.Serialize(data)); }
public bool IsReusable { get { return false; } } ---HTML---- $('#cc2').combobox({ url: "Handler1.ashx", valueField: 'Id', textField: 'TextName', panelWidth: 650, panelHeight: 'auto', formatter: formatItem }); function formatItem(row) { var s = "<table><tr><td width='50px'><b>" + row.TextName + "</b></td><td width='600px'>" + row.Desc + "</td></tr></table>" return s; } --Solution 2: use .aspx file ----- ---ASPX------- <%@ Page Language="C#" AutoEventWireup="true" %>
<% String x = ""; x = "[{ \"Id\":1, \"TextName\":\"Java\", \"Desc\":\"Write once, run anywhere\"},{ \"Id\":2, \"TextName\":\"C#\", \"Desc\":\"hi\" }]"; Response.Write(x); %> ----HTML------- $('#cc').combobox({ url: 'WebForm2.aspx', method: 'get', valueField: 'Id', textField: 'TextName', panelWidth: 650, panelHeight: 'auto', formatter: function (row) { var s = "<table><tr><td width='50px'><b>" + row.TextName + "</b></td><td width='600px'>" + row.Desc + "</td></tr></table>" return s; } }); So I don't know why it cannot get data from Web Service. Does anyone know the reason please let me know. Regards, KwFreeLife.
|