Can I allow the client and server to operate on the same json object? The current method is to hide controls on the client.
The following is a List of generic List objects <TrainingImplement>. After the object is converted to json, how does the client and server perform operations?
1. Code Conversion between json objects and C # generics
Copy codeThe Code is as follows:
// Convert json data to a generic type
Public static T ConvertByteDataToObject <T> (string byteData)
{
T obj;
Using (var MS = new MemoryStream (Encoding. UTF8.GetBytes (byteData )))
{
Var serializer = new DataContractJsonSerializer (typeof (T ));
Obj = (T) serializer. ReadObject (MS );
}
Return obj;
}
// Convert the generic type to json
Public static string ConvertObjectToByteData <T> (T obj)
{
String result;
Using (var MS = new MemoryStream ())
{
Var serializer = new DataContractJsonSerializer (typeof (T ));
Serializer. WriteObject (MS, obj );
Ms. Position = 0;
Result = Encoding. UTF8.GetString (ms. ToArray ());
}
Return result;
}
2. The json data source is stored in the hidden control of the client.
Copy codeThe Code is as follows:
<Input type = "hidden" id = "hideDataSource" runat = "server"/>
3. After hiding the json data in the control, the request may be incorrect because it contains "/". Therefore, set ValidateRequest = "false" in the page header"
Copy codeThe Code is as follows:
<% @ Page Language = "C #" ValidateRequest = "false" AutoEventWireup = "true"
4. initialize the json data source when Page_Load is loaded on the page.
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
# Region loading data sources
If (! IsPostBack)
{
List <TrainingImplement> list = new List <TrainingImplement> (){
New TrainingImplement (){
Code = "aaa ",
C_name = "bbb"
}
....
} // Initialize the data source
HideDataSource. Value = ConvertObjectToByteData (list );
}
Else
{
// If the data is returned, the data source is read from the client.
List <TrainingImplement> list = ConvertByteDataToObject <List <TrainingImplement> (hideDataSource. Value );
HideDataSource. Value = ConvertObjectToByteData (list );
}
# Endregion
4. Example of json Data Source Operation in client js
Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "../Scripts/jquery-1.4.3.js"> </script>
<Script type = "text/javascript" src = "../Scripts/jquery-ui-1.8.7.custom.min.js"> </script>
<Script type = "text/javascript" src = "../Scripts/jquery. json-2.2.min.js"> </script>
<Script type = "text/javascript">
Var cecehidname = "hideDataSource ";
Var performancedom;
Var cecejson;
$ (Document). ready
(Function (){
// Obtain the data source
Performancedom = document. getElementById (performancehidname );
Performancejson = eval ("(" + performancedom. value + ")");
});
// Example Method for modifying the Code value of 1st TrainingImplement objects
Function ModifiedCode (){
Performancejson [0]. _ code = "Code001 ";
// Write the updated json object to the hidden control again.
Performancedom. value = $. toJSON (performancejson );
}
</Script>
5. Server C # operate the Json Data Source
Copy codeThe Code is as follows:
List <TrainingImplement> list = ConvertByteDataToObject <List <TrainingImplement> (hideDataSource. Value );