2011-11-12 3 views
0

데이터를 jquery에 바인딩하기 위해 dropdownlists에 바인딩하고 있습니다. & 또한 웹 서비스 메서드를 사용하고 작업도 훌륭하지만 데이터 바인딩을위한 명확한 설명이 없습니다. 하나의 드롭 다운 목록 웹 메서드에서 데이터를 가져 오는 중 Array 개체를 반환하고 다른 드롭 다운 목록에 대한 JSON 개체 측면에서 반환하는 웹 메서드에서 데이터를 가져 오는 중이지만 프런트 엔드에서 나는 어떤 차이가 있어요. 그들 중 대부분은 serialize 된 json에게 작업 할 좋은 방법을 알려주므로 여기서는 실제로 어떻게됩니까? 나는 비트는 시스템에 의해 JSON 배열로 변환하는 배열을 반환하는 가 첫 번째 전화에서 당신에게ToArray와 JSON의 차이점을 이해하십시오

**here is my code** 
Default.aspx 

<html> 
<head runat="server"> 
    <title>JsonAndToArray</title> 

    <script type="text/javascript" src="js/jquery.min.js"></script> 

    <script type="text/javascript"> 

     $(document).ready(function() { 
      ddlActivityType = document.getElementById("<%=ddlActivity.ClientID %>"); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json;charset/utf-8", 
       url: "Visit.asmx/GetActivityByJSON", 
       dataType: "json", 
       success: function(results) { 
        results = eval(results.d); 
        $('#ddlActivity').get(0).options.length = 0; 
        $('#ddlActivity').get(0).options[0] = new Option(' --select-- ', '0'); 
        $.each(results, function(val, text) { 
         $('#ddlActivity').append($('<option></option>').val(text[1]).html(text[0])); 
        }); 
       } 
      }); 

      ddlActivityType1 = document.getElementById("<%=ddlActivity2.ClientID %>"); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json;charset/utf-8", 
       url: "Visit.asmx/GetActivity", 
       dataType: "json", 
       success: function(results) { 
        results = eval(results.d); 
        $('#ddlActivity2').get(0).options.length = 0; 
        $('#ddlActivity2').get(0).options[0] = new Option('--select--', '0'); 
        $.each(results, function(val, text) { 
         $('#ddlActivity2').append($('<option></option>').val(text[1]).html(text[0])); 
        }); 
       } 
      }); 
     }); 

    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    Json-Activity : 
    <select id="ddlActivity" runat="server"> 
    </select> 
    <br /> 
    <br /> 
    ToArray-Activity : 
    <select id="ddlActivity2" runat="server"> 
    </select> 
    <br /> 
    <br /> 
    <asp:Button ID="btnJson" runat="server" Text="Json" OnClick="Json_click"/> 
    </form> 
</body> 
</html> 


Defalut.aspx.cs 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Script.Serialization; 

public partial class Defalut: System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Json_click(object sender,EventArgs e) 
    { 
    } 
} 


**webservices** 

** 

- Visit.asmx 

** 


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Script.Serialization; 
using Facade; 

/// <summary> 
/// Summary description for Visit 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 
public class Visit : System.Web.Services.WebService 
{ 

    public Visit() 
    { 

     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 

    [WebMethod] 
    public string HelloWorld() 
    { 
     return "Hello World"; 
    } 

    [WebMethod] 
    public IList<string[]> GetActivity() 
    { 
     IList<string[]> values = new List<string[]>(); 
     //string value = ""; 
     try 
     { 
      SqlConnection con_New = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog="Database";Integrated Security=True;"); 
      con_New.Open(); 
      SqlCommand cmdSelect_ST = new SqlCommand("select id,name from table", con_New); 
      SqlDataAdapter da_ST = new SqlDataAdapter(cmdSelect_ST); 

      DataSet ds = new DataSet(); 
      da_ST.Fill(ds); 
      DataTable dt = ds.Tables[0]; 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       string[] ActivityType = new string[2]; 
       ActivityType[0] = dt.Rows[i]["name"].ToString(); 
       ActivityType[1] = dt.Rows[i]["id"].ToString(); 
       values.Add(ActivityType); 
      }   
     } 
     catch (Exception ex) 
     { 

     } 
     return values; 
    } 


    [WebMethod] 
    public string GetActivityByJSON() 
    { 
     IList<string[]> values = new List<string[]>(); 
     string value = ""; 
     try 
     { 
      SqlConnection con_New = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog="Database";Integrated Security=True;"); 
      con_New.Open(); 
      SqlCommand cmdSelect_ST = new SqlCommand("select name,id from table", con_New); 
      SqlDataAdapter da_ST = new SqlDataAdapter(cmdSelect_ST); 

      DataSet ds = new DataSet(); 
      da_ST.Fill(ds); 
      DataTable dt = ds.Tables[0]; 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       string[] ActivityType = new string[2]; 
       ActivityType[0] = dt.Rows[i]["name"].ToString(); 
       ActivityType[1] = dt.Rows[i]["id"].ToString(); 
       values.Add(ActivityType); 
      } 
      JavaScriptSerializer js = new JavaScriptSerializer(); 
      value = js.Serialize(values); 
     } 
     catch (Exception ex) 
     { 

     } 
     return value; 
    } 
} 

답변

0

감사 저를 도와주세요 혼동하고있다. 이를 반환 :

IList<string[]> values = new List<string[]>(); 

을 당신이 jsaon 배열로 변환하여 문자열로 반환 두 번째 호출에서. - 차이가 없다 JSON 이후

JavaScriptSerializer js = new JavaScriptSerializer(); 
value = js.Serialize(values); 

단지 특정 방법으로 포맷 된 문자열입니다 : 여기에 변환을 수행하는 코드이다. 되돌려 보낼 문자열이 만들어지고 누가 (코드 또는 시스템 코드) 작성하는지.

+0

json을 serialize하고 프런트 엔드로 다시 전송해야 할 필요가 없으며 대신에 To Array를 사용하여 이해할 수 있습니까? –

+0

음 ... 일종의. 시스템에서 json이 사용자를 위해 일련 화합니다. 'ToArray()'는 아무 것도하지 않습니다. – Hogan

+0

오 ... 그런 식으로, 감사합니다 –