2011-09-12 99 views

回答

0

DOM元素標準屬性的值可以是滿足相關HTML屬性要求的任何值。選件元件的value attribute必須符合cdata的要求。

0

這裏是我希望它解決你的問題的代碼。 這是web服務.asmx擴展類型。

public class WebService1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<Employee> SayHellowJson(string name) 
    { 
     //string result = string.Format(" {1} : {0} ",name,DateTime.Now); 
     Employee emp = new Employee{ Name= name , Designation = "Senior Software Engineer" }; 
     List<Employee> employees = new List<Employee>(); 
     employees.Add(emp); 
     employees.Add(new Employee { Name = "first", Designation = "designation" }); 
     employees.Add (new Employee { Name="second", Designation="Designation2" }); 
     employees.Add(new Employee {Name="thired" , Designation="Deisgnation4" }); 

     //JavaScriptSerializer serializer = new JavaScriptSerializer(); 

     //return serializer.Serialize(emp); 
     return employees; 
    } 
} 
[Serializable] 
public class Employee 
{ 
    public string Name { get; set; } 
    public string Designation { get; set; } 
} 
} 

存在對jQuery的/和HTML代碼... 我綁定JSON調用輸入keyup事件是指下拉填充當U按下texbox鍵。 這裏是代碼 <%@頁面語言= 「C#」 AutoEventWireup = 「真」 代碼隱藏= 「JsonCall.aspx.cs」 繼承= 「WebApplication1.JsonCall」 %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <script type="text/javascript"> 
$(function(){ 
$('#txtname').keyup(function(evt) 
{ 
    $.ajax({ 
    url: "/WebService1.asmx/SayHellowJson", 
    type: "POST", 
    dataType:"json", 
    data:"{name:'"+Name+"'}", 
    contentType : "application/json; charset=utf-8", 
    success: function(msg){ 
     for (var i = 0 ; i < msg['d'].length ; i ++){ 
       $('#myselect').append('<option value="'+msg['d'][i].Name+'">'+msg['d'][i].Name+'</option>'); 
     } 

     }, 
    error : function(e){ 
      alert ("error "); 
     } 
    }); 
}); 

}); 
    </script> 

</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <p> 
     JSON format call</p> 
    Enter Name : 
    <input type="text" id="txtname" /> 
    <input type="button" value="GO" id="btnGO" /> 
    <br /> 
    <p id='result'> 
    </p> 
    </div> 
    </form> 
    <select id="myselect" name="D1"> 
     <option></option> 
    </select> 
</body> 
</html> 
相關問題