General Handler Jsondate.ashx
public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType =
"Text/plain"
;
DataTable data2 = MyData ();
String str = Datatabletojson (
"Myjson"
, data2);
Built JSON data
context.htm = htm& (str);
}
public bool IsReusable
{
Get
{
Return
False
;
}
}
Using a DataTable to do data, data simulation
Public DataTable MyData ()
{
DataTable dt =
New
DataTable ();
DataColumn DC;
DataRow Dr;
DC =
New
DataColumn (
"Name"
, System.Type.GetType (
"System.String"
));
Dt. Columns.Add (DC);
DC =
New
DataColumn (
"Email"
, System.Type.GetType (
"System.String"
));
Dt. Columns.Add (DC);
DC =
New
DataColumn (
"Phon"
, System.Type.GetType (
"System.String"
));
Dt. Columns.Add (DC);
DC =
New
DataColumn (
"Move"
, System.Type.GetType (
"System.String"
));
Dt. Columns.Add (DC);
DC =
New
DataColumn (
"Pan"
, System.Type.GetType (
"System.String"
));
Dt. Columns.Add (DC);
DC =
New
DataColumn (
"Select"
, System.Type.GetType (
"System.String"
));
Dt. Columns.Add (DC);
For
(int i = 1; i < 7; i++)
{
Dr = dt. NewRow ();
dr[
"Name"
] =
"Name"
+ i.tostring ();
dr[
"Email"
] =
"Email"
+ i.tostring ();
dr[
"Phon"
] =
"Phon"
+ i.tostring ();
dr[
"Move"
] =
"Move"
+ i.tostring ();
dr[
"Pan"
] =
"Pan"
+ i.tostring ();
dr[
"Select"
] = i.ToString ();
Dt. Rows.Add (DR);
}
int count = dt. Rows.Count;
Return
Dt
}
Converting DataTable data to JSON data
public string Datatabletojson (string jsonname, DataTable DT)
{
StringBuilder Json =
New
StringBuilder ();
Json.append (
"["
);
If
(dt. Rows.Count > 0)
{
For
(int i = 0; i < dt.) Rows.Count; i++)
{
Json.append (
"{"
);
For
(int j = 0; j < dt.) Columns.count; J + +)
{
Json.append (dt. COLUMNS[J]. Columnname.tostring () +
":""
+ dt. ROWS[I][J]. ToString () +
"""
);
If
(J < dt.) COLUMNS.COUNT-1)
{
Json.append (
","
);
}
}
Json.append (
"}"
);
If
(i < dt.) ROWS.COUNT-1)
{
Json.append (
","
);
}
}
}
Json.append (
"]"
);
Return
Json.tostring ();
}
To convert list data to JSON data
public string objecttojson<t> (string jsonname, ilist<t> IL)
{
StringBuilder Json =
New
StringBuilder ();
Json.append (
"["
);
If
(IL. Count > 0)
{
For
(int i = 0; i < IL.) Count; i++)
{
T obj = activator.createinstance<t> ();
Type type = obj. GetType ();
propertyinfo[] PiS = type. GetProperties ();
Json.append (
"{"
);
For
(int j = 0; J < PiS.) Length; J + +)
{
Json.append (Pis[j]. Name.tostring () +
":""
+ Pis[j]. GetValue (Il[i],
Null
) +
"""
);
If
(J < PiS.) LENGTH-1)
{
Json.append (
","
);
}
}
Json.append (
"}"
);
If
(I < IL. COUNT-1)
{
Json.append (
","
);
}
}
}
Json.append (
"]"
);
Return
Json.tostring ();
}
Page Default.aspx
function
GetData ()
{
$.getjson (
"Jsondata.ashx"
,
Server-side pages that generate JSON data
function
(JSON)
{
For
(
Var
i=0;i<json.length;i++)
{
Begin
Var
Index=-1;
Var
Txttrlastindex = Findobj (
"Txttrlastindex"
, document);
Var
RowID = parseint (Txttrlastindex.value);
Var
Signframe = Findobj (
"Signframe"
, document);
Add rows
Var
NEWTR = Signframe.insertrow (signFrame.rows.length);
Newtr.id =
"Signitem"
+ RowID;
Add Column: Ordinal
Var
Newnametd=newtr.insertcell (0);
Add Column Contents
newnametd.innerhtml = NewTR.rowIndex.toString ();
Add Column: Name
Var
Newnametd=newtr.insertcell (1);
Add Column Contents
newnametd.innerhtml =
"<input name= ' Txtname"
+ RowID +
"' Id= ' txtname"
+ RowID +
"' Value= '"
+json[i]. name+
"' type= ' text ' size= '/>"
;
Adding columns: E-mail
Var
Newemailtd=newtr.insertcell (2);
Add Column Contents
newemailtd.innerhtml =
"<input name= ' Txtemail"
+ RowID +
"' Id= ' Txtemail"
+ RowID +
"' Value= '"
+json[i]. email+
"' type= ' text ' size= '/>"
;
Add Column: Phone
Var
Newteltd=newtr.insertcell (3);
Add Column Contents
newteltd.innerhtml =
"<input name= ' Txttel"
+ RowID +
"' Id= ' Txttel"
+ RowID +
"' Value= '"
+json[i]. phon+
"' type= ' text ' size= '/>"
;
Add Column: Mobile phone
Var
Newmobiletd=newtr.insertcell (4);
Add Column Contents
newmobiletd.innerhtml =
"<input name= ' Txtmobile"
+ RowID +
"' Id= ' Txtmobile"
+ RowID +
"' Value= '"
+json[i]. Move+
"' type= ' text ' size= '/>"
;
Add Column: Company name
Var
Newcompanytd=newtr.insertcell (5);
Add Column Contents
newcompanytd.innerhtml =
"<input name= ' Txtcompany"
+ RowID +
"' Id= ' Txtcompany"
+ RowID +
"' Value= '"
+json[i]. pan+
"' type= ' text ' size= '/>"
;
Add drop down box
Var
Newcompanytd=newtr.insertcell (6);
newcompanytd.innerhtml =
"<select id= ' select"
+rowid+
"' ><option value= ' 1 ' >1</option><option value= ' 2 ' >2</option><option value= ' 3 ' >3 </option><option value= ' 4 ' >4</option><option value= ' 5 ' >5</option> </select> '
;
Select the Drop-down box dynamically
For
(
Var
J=0;j<document.getelementbyid (
"Select"
+ROWID). options.length;j++)
{
If
(document.getElementById (
"Select"
+ROWID). Options[j].text==json[i].select)
Index=j;
}
If
(index>=0)
document.getElementById (
"Select"
+ROWID). options[index].selected=
True
;
Add Column: Delete button
Var
Newdeletetd=newtr.insertcell (7);
Add Column Contents
newdeletetd.innerhtml =
"<div align= ' center ' style= ' width:40px ' ><a href= ' javascript:; ' onclick= ' Deletesignrow (' Signitem ')
+ RowID +
"')" > Delete </a></div> "
;
Push line number to next line
Txttrlastindex.value = (RowID + 1). ToString ();
}
}
)
}
The transformation of DataTable and JSON 1 Converting a DataTable or ilist<> to JSON format Using System;
Using System.Data;
Using System.Text;
Using System.Collections.Generic;
Using System.Reflection;
<summary>
Convert a DataTable or ilist<> into JSON format
</summary>
public class Tojson
{
Public Tojson ()
{
}
The DataTable turns into JSON
public static string Datatabletojson (String jsonname, DataTable DT)
{
StringBuilder Json =
New
StringBuilder ();
Json.append (
"{""
+ Jsonname +
"":["
);
If
(dt. Rows.Count > 0)
{
For
(int i = 0; i < dt.) Rows.Count; i++)
{
Json.append (
"{"
);
For
(int j = 0; j < dt.) Columns.count; J + +)
{
Json.append (
"""
+ dt. COLUMNS[J]. Columnname.tostring () +
"":""
+ dt. ROWS[I][J]. ToString () +
"""
);
If
(J < dt.) COLUMNS.COUNT-1)
{
Json.append (
","
);
}
}
Json.append (
"}"
);
If
(i < dt.) ROWS.COUNT-1)
{
Json.append (
","
);
}
}
}
Json.append (
"]}"
);
Return
Json.tostring ();
}
The list turns into JSON
public static string Objecttojson<t> (String jsonname, ilist<t> IL)
{
StringBuilder Json =
New
StringBuilder ();
Json.append (
"{""
+ Jsonname +
"":["
);
If
(IL. Count > 0)
{
For
(int i = 0; i < IL.) Count; i++)
{
T obj = activator.createinstance<t> ();
Type type = obj. GetType ();
propertyinfo[] PiS = type. GetProperties ();
Json.append (
"{"
);
For
(int j = 0; J < PiS.) Length; J + +)
{
Json.append (
"""
+ Pis[j]. Name.tostring () +
"":""
+ Pis[j]. GetValue (Il[i],
Null
) +
"""
);
If
(J < PiS.) LENGTH-1)
{
Json.append (
","
);
}
}
Json.append (
"}"
);
If
(I < IL. COUNT-1)
{
Json.append (
","
);
}
}
}
Json.append (
"]}"
);
Return
Json.tostring ();
}
} |