簡體   English   中英

將datatable轉換為json並在aspx中綁定控件

[英]Convert datatable to json and bind in aspx for controls

我有一個datatable對象,該對象根據某種條件返回1。 我想是的,我想填補這些datatable從控制值aspx側。 為此,我想將這些數據表轉換為json 如何將其轉換並綁定到aspx頁面?

這是我嘗試的代碼:

protected void GET_VSAT_FORM_DATA(string SapId, string CandidateId)
{ 
    try
    {
        DataTable dtGetData = new DataTable();
        CommonDB CDB = new CommonDB();
        dtGetData = CDB.GET_VSAT_DATA(SapId, CandidateId);
    }
    catch (Exception)
    {   
        throw;
    }
}

UPDATE

<span>CIRCLE :</span>
    <input type="text" id="txtCircle" style="width: auto;" readonly="true" />
    <br />

    <span>CANDIDATE ID :</span>
    <input type="text" id="txtCandidate" style="width: auto;" readonly="true" />
    <br />

    <span>SITE ID :</span>
    <input type="text" id="txtSiteId" style="width: auto;" readonly="true" />
    <br />

    <span>PRIORITY ID :</span>
    <input type="text" id="txtPriority" style="width: auto;" readonly="true" />
    <br />

    <span>SITE NAME :</span>
    <input type="text" id="txtSiteName" style="width: auto;" readonly="true" />
    <br />

    <span>SAP ID :</span>
    <input type="text" id="txtSapId" style="width: auto;" readonly="true" />
    <br />

您可以使用以下方法將dataTable轉換為JSON, 來源

public string DataTableToJSONWithJavaScriptSerializer(DataTable table) 
{  
      JavaScriptSerializer jsSerializer = new JavaScriptSerializer();  
      List < Dictionary < string, object >> parentRow = new List < 
      Dictionary < string, object >> ();  
      Dictionary < string, object > childRow;  
      foreach(DataRow row in table.Rows) 
      {  
         childRow = new Dictionary < string, object > ();  
         foreach(DataColumn col in table.Columns) 
         {  
              childRow.Add(col.ColumnName, row[col]);  
         }  
         parentRow.Add(childRow);  
      }  

     return jsSerializer.Serialize(parentRow);  
} 

這將在類中的其他位置返回JSON string.place上面的方法,並像下面這樣調用下面的方法

var convertedJSON = DataTableToJSONWithJavaScriptSerializer(dtGetData);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM