Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" %>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <body>
  5. <form runat="server">
  6. <div id="Feedback"></div>
  7. <div><a href="#" id="ExportButton">Export</a></div>
  8. <div><a href="#" id="ImportButton">Import</a></div>
  9. </form>
  10. <script src="js/jquery-2.1.1.min.js"></script>
  11. <script>
  12. function Export() {
  13. $.ajax({
  14. type: "POST",
  15. url: 'default.aspx/GetThings',
  16. contentType: "application/json; charset=u",
  17. dataType: "json",
  18. async: true,
  19. success: function (result) {
  20. var json = result.d;
  21.  
  22. $('#Feedback').html(json);
  23.  
  24. // failed attempt #1
  25. //localStorage["things"] = json;
  26.  
  27. // failed attempt #2
  28. var things = jQuery.parseJSON(json);
  29. localStorage["things"] = JSON.stringify(things);
  30. }
  31. });
  32. }
  33.  
  34. function Import() {
  35. $.ajax({
  36. type: "POST",
  37. url: "default.aspx/PutThings",
  38. data: "{'json':'" + localStorage["things"] + "'}",
  39. contentType: "application/json; charset=u",
  40. dataType: "json",
  41. async: true,
  42. success: function (result) {
  43. $('#Feedback').html(result.d);
  44. }
  45. });
  46. }
  47.  
  48. $(function () {
  49. $("#ExportButton").click(function () {
  50. Export();
  51. });
  52.  
  53. $("#ImportButton").click(function () {
  54. Import();
  55. });
  56. });
  57. </script>
  58. </body>
  59. </html>
  60.  
  61. using System;
  62. using System.Collections.Generic;
  63. using System.Runtime.Serialization;
  64. using System.Text;
  65. using System.Web.Script.Serialization;
  66. using System.Web.Services;
  67. using System.Web.UI;
  68.  
  69. public partial class _default : Page
  70. {
  71. [WebMethod]
  72. public static string GetThings()
  73. {
  74. List<Thing> things = new List<Thing>();
  75.  
  76. Thing thing = new Thing();
  77. //thing.Description = "no escaped double quote string works fine";
  78. thing.Description = "100 (2 1/8") Gear";
  79. things.Add(thing);
  80.  
  81. JavaScriptSerializer serializer = new JavaScriptSerializer();
  82.  
  83. StringBuilder json = new StringBuilder();
  84.  
  85. serializer.Serialize(things, json);
  86.  
  87. return json.ToString();
  88. }
  89.  
  90. [WebMethod]
  91. public static string PutThings(string json)
  92. {
  93. try
  94. {
  95. JavaScriptSerializer serializer = new JavaScriptSerializer();
  96.  
  97. List<Thing> things = serializer.Deserialize<List<Thing>>(json);
  98.  
  99. return things[0].Description;
  100. }
  101. catch (Exception e)
  102. {
  103. return e.Message;
  104. }
  105. }
  106.  
  107. public class Thing
  108. {
  109. [DataMember]
  110. public string Description { get; set; }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement