Created
July 24, 2009 19:33
-
-
Save ntulip/154493 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Extensions | |
{ | |
/// <summary> | |
/// Converts an object to a client side Json Object | |
/// </summary> | |
/// <param name="obj"></param> | |
/// <returns></returns> | |
public static string ToJson(this object obj) | |
{ | |
JavaScriptSerializer ser = new JavaScriptSerializer(); | |
return ser.Serialize(obj); | |
} | |
/// <summary> | |
/// Converts an object to a client side Json Object - allows for recursion specifications | |
/// </summary> | |
/// <param name="obj"></param> | |
/// <param name="recursionDepth"></param> | |
/// <returns></returns> | |
public static string ToJson(this object obj, int recursionDepth) | |
{ | |
JavaScriptSerializer ser = new JavaScriptSerializer(); | |
ser.RecursionLimit = recursionDepth; | |
return ser.Serialize(obj); | |
} | |
public static string ToJson(this List<object> obj) | |
{ | |
return new | |
{ | |
totalCount = obj.Count, | |
rows = obj | |
}.ToJson(); | |
} | |
/// <summary> | |
/// DataTable to HTML - apply styles please .styledTable .styledTable td, .styledTableHeader .styledTableRow, .styledTableAltRow | |
/// </summary> | |
/// <param name="table"></param> | |
/// <returns></returns> | |
public static string ToHtmlTable(this DataTable table) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine("<table class=\"styledTable\" cellspacing=\"0\">"); | |
sb.AppendLine("<tr>"); | |
for (int i = 0; i <= table.Columns.Count - 1; i++) | |
{ | |
sb.AppendLine("<td class=\"styledTableHeader\">" + table.Columns[i].ColumnName + "</td>"); | |
} | |
sb.AppendLine("</tr>"); | |
for (int i = 0; i <= table.Rows.Count - 1; i++) | |
{ | |
sb.AppendLine("<tr>"); | |
for (int j = 0; j <= table.Columns.Count - 1; j++) | |
{ | |
if (i % 2 == 0) | |
sb.AppendLine("<td class=\"styledTableRow\">" + table.Rows[i][j].ToString() + "</td>"); | |
else | |
sb.AppendLine("<td class=\"styledTableAltRow\">" + table.Rows[i][j].ToString() + "</td>"); | |
} | |
sb.AppendLine("</tr>"); | |
} | |
sb.AppendLine("<table>"); | |
return sb.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment