Datatable converts the data into Json and puts the Json data into the js file.

Source: Internet
Author: User
Tags tojson

Datatable converts the data into Json and puts the Json data into the js file.

5 code instances in C # converting Datatable to Json

////// Datatable to Json //////Datatable object///
 
  
Json string
 Public static string ToJson (DataTable dt) {StringBuilder jsonString = new StringBuilder (); jsonString. append ("["); DataRowCollection drc = dt. rows; for (int I = 0; I <drc. count; I ++) {jsonString. append ("{"); for (int j = 0; j <dt. columns. count; j ++) {string strKey = dt. columns [j]. columnName; string strValue = drc [I] [j]. toString (); Type type = dt. columns [j]. dataType; jsonString. append ("\" "+ strKey +" \ ":"); strValue = StringFormat (strValue, type); if (j <dt. columns. count-1) {jsonString. append (strValue + ",");} else {jsonString. append (strValue) ;}} jsonString. append ("},");} jsonString. remove (jsonString. length-1, 1); jsonString. append ("]"); return jsonString. toString ();}////// Format String, date, and Boolean ////////////
 Private static string StringFormat (string str, Type type) {if (type = typeof (string) {str = String2Json (str ); str = "\" "+ str +" \ "";} else if (type = typeof (DateTime )) {str = "\" "+ str +" \ "";} else if (type = typeof (bool) {str = str. toLower ();} else if (type! = Typeof (string) & string. IsNullOrEmpty (str) {str = "\" "+ str +" \ "" ;}return str ;}////// Filter special characters //////String///
 
  
Json string
 Private static string String2Json (String s) {StringBuilder sb = new StringBuilder (); for (int I = 0; I <s. length; I ++) {char c = s. toCharArray () [I]; switch (c) {case '\ "': sb. append ("\" "); break; case '\': sb. append ("\\\\"); break; case '/': sb. append ("\/"); break; case '\ B': sb. append ("\ B"); break; case '\ F': sb. append ("\ f"); break; case '\ N': sb. append ("\ n"); break; case '\ R': sb. append ("\ r"); break; case '\ t': sb. append ("\ t"); break; default: sb. append (c); break;} return sb. toString ();}


<2>

// Query the province name var SQL = "select top 35 locid, locname, parentid from location where loctype = 1"; var dt_pro = SQLHelper. get_DataTable (SQL, SQLHelper. getCon (), new Dictionary
 
  
(); // Convert the dt_pro DataTable to json var tt_pro = ToJson (dt_pro); // query the city name var vsql = "select locid, locname, parentid from location where parentid in (select top 35 locId from location where loctype = 1) "; var dt_city = SQLHelper. get_DataTable (vsql, SQLHelper. getCon (), new Dictionary
  
   
(); // Convert the dt_city DataTable to json var tt_city = ToJson (dt_city );
  
 

<3> Create a js file and write two variables var cn_pro in the file. Assign the tt_pro value to cn_pro and the tt_city value to cn_city.

For example, enter var cn_pro = [{"locid": 1, "locname": "Beijing", "parentid": 0 },{ "locid": 673, "locname": "Shanghai", "parentid": 0}];

Var cn_city = [{"locid": 2, "locname": "Beijing", "parentid": 1 },{ "locid": 674, "locname ": "Shanghai", "parentid": 673}]


Of course, this is short for me. Most of the content is removed. The first two data entries are retained as an example.


========================================================== ======================================

<% @ Page Title = "\" Language = "C #" MasterPageFile = "~ /Views/Shared/MemberCenter. Master "Inherits =" System. Web. Mvc. ViewPage
 
  
"%> <% @ Import Namespace =" bigtree "%> <% using (Html. BeginForm () {%> Personal Data
  
 
  • Mobile phone:

    <% ---- %> <%: Html. textBoxFor (x => x. mobile, new {@ class = "setterInput fl", @ readonly = ""}) %>)
  • Name:

    <% ---- %> <%: Html. TextBoxFor (x => x. Name, new {@ class = "setterInput fl"}) %>
  • Email:

    <% ---- %> <%: Html. textBoxFor (x => x. email, new {@ class = "setterInput fl"}) %> <% var I = Model. emailFlag; if (I = true) {%> verified <%}%> <% else {%> not verified <%}%>
  • Nickname:

    <% ---- %> <%: Html. TextBoxFor (x => x. UserName, new {@ class = "setterInput fl"}) %>
  • Gender:

    <% ---- %> <%: Html. RadioButtonFor (x => x. Sex, "male", new {@ name = "sex"}) %> male <% ---- %> <%: Html. RadioButtonFor (x => x. Sex, "female", new {@ name = "sex"}) %> female
  • <% Var province = ViewData ["province"] as System. Data. DataTable; %>
  • Common cities:

    Beijing

    Save

    Beijing

    City
<% }%><Script type = "text/javascript"> $ (function () {$ (". navList>: contains ('profile ')"). addClass ("cur"); // set province var proList = ""; $. each (cn_pro, function (I, kv) {proList + ="
  • "+ Kv. locname +"
  • ";}); $ (" # ProList ").html (proList); $ (" # proList li "). bind ("click", function () {var li = $ (this); var cityList = ""; $. each (cn_city, function (I, kv) {if (kv. parentid = li. attr ("data-id") {cityList + ="
  • "+ Kv. locname +"
  • ";}}); $ (" # CityList ").html (cityList); $ (" # displyPro "). text (li. text (); $ ("# displyCity "). text ($ ("# cityList "). children (). first (). text (); $ ("# selectCityId "). val ($ ("# cityList "). children (). first (). attr ("data-id"); $ ("# selectCityName "). val ($ ("# cityList "). children (). first (). text (); $ ("# cityList li "). bind ("click", function () {$ ("# displyCity "). text ($ (this ). text (); $ ("# selectCityId "). val ($ (this ). attr ("data-id"); $ ("# selectCityName "). val ($ (this ). text () ;}) ;};}) </script> <script type = "text/javascript" src = ".. /.. /Scripts/Location. js "> </script>


    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.