HTML Class for C#


/ Published in: C#
Save to your folder(s)

This class uses static methods to create html controls on the fly in .NET


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections.Generic;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. /// <summary>
  13. /// Summary description for HTML
  14. /// </summary>
  15. public class HTML
  16. {
  17. public HTML()
  18. {
  19. //
  20. // TODO: Add constructor logic here
  21. //
  22. }
  23. public static string link(string url, string title, string cssClass, string javascript)
  24. {
  25. HTML mHt = new HTML();
  26. return "<a href=\"" + url + "\"" + mHt.getCssClass(cssClass) + mHt.getJavascript(javascript) + " >" + title + "</a>\n";
  27. }
  28.  
  29. public static string link(string url, string title, string cssClass)
  30. {
  31. return HTML.link(url, title, cssClass, "");
  32.  
  33. }
  34.  
  35. public static string link(string url, string title)
  36. {
  37. return HTML.link(url, title, "", "");
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. public static string input(string id, string value, int maxLength , string cssClass, string javascript)
  46. {
  47. HTML mHt = new HTML();
  48.  
  49. string strMaxLength = "";
  50. if (maxLength != -1)
  51. {
  52. strMaxLength = "size=\"" + maxLength.ToString() + "\" maxlength=\"" + maxLength.ToString() +"\"";
  53. }
  54. return "<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\" " + strMaxLength + "value=\"" + value + "\" " + mHt.getCssClass(cssClass) + mHt.getJavascript(javascript) + "/>\n";
  55. }
  56.  
  57. public static string input(string id, string value, int maxLength, string cssClass)
  58. {
  59. return HTML.input(id, value, maxLength, cssClass, "");
  60. }
  61.  
  62. public static string input(string id, string value, int maxLength)
  63. {
  64. return HTML.input(id, value, maxLength, "", "");
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. public static string submit(string id, string value, string cssClass, string javascript)
  72. {
  73. HTML mHt = new HTML();
  74. return "<input type=\"submit\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + value + "\" " + mHt.getCssClass(cssClass) + mHt.getJavascript(javascript) + "/>\n";
  75. }
  76.  
  77. public static string submit(string id, string value, string cssClass)
  78. {
  79. return HTML.submit(id, value, cssClass, "");
  80. }
  81.  
  82. public static string submit(string id, string value)
  83. {
  84. return HTML.submit(id, value, "", "");
  85. }
  86.  
  87.  
  88. public static string drop(string id, string[,] data, string selectedValue, string cssClass, string javascript)
  89. {
  90. HTML mHt = new HTML();
  91.  
  92. if (data != null)
  93. {
  94. data[0, 0] = "";
  95.  
  96. if (data.GetUpperBound(1) > 0)
  97. {
  98. data[0, 1] = "";
  99. }
  100. }
  101.  
  102. string dropString = "<select name=\"" + id + "\" id=\"" + id + "\" " + mHt.getCssClass(cssClass) + " " + mHt.getJavascript(javascript) + " >\n";
  103.  
  104. for (int i = 0; i < data.GetUpperBound(0) + 1 && data.GetUpperBound(1) == 1; ++i)
  105. {
  106. String curDesc = data[i, 1].ToString().Replace(" ", "&nbsp;");
  107. string selected = selectedValue == data[i, 0].ToString() ? " selected=selected " : "";
  108.  
  109. while (curDesc.IndexOf(" ") > -1)
  110. {
  111. curDesc = curDesc.Replace(" ", "&nbsp;");
  112. }
  113. dropString += "<option value=\"" + data[i, 0].ToString() + "\" " + selected + ">" + curDesc + "</option>\n";
  114. }
  115. dropString += "</select>\n";
  116.  
  117. return dropString;
  118.  
  119. }
  120.  
  121. public static string drop(string id, string[,] data, string selectedValue, string cssClass)
  122. {
  123. return HTML.drop(id, data, selectedValue, cssClass, "");
  124. }
  125.  
  126. public static string drop(string id, string[,] data, string selectedValue)
  127. {
  128. return HTML.drop(id, data, selectedValue, "", "");
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. public static string image(string src, string alt, string css, string javascript)
  139. {
  140. HTML mHt = new HTML();
  141. return "<img src=\"" +src + "\" alt=\"" + alt + "\" " + mHt.getCssClass(css) + " " + mHt.getJavascript(javascript) + ">\n";
  142. }
  143.  
  144.  
  145. public static string list(string id, string[,] data, string cssClass, string javascript)
  146. {
  147. if (data != null)
  148. {
  149. data[0, 0] = "";
  150. //data[0, 1] = "";
  151. }
  152.  
  153. HTML mHt = new HTML();
  154.  
  155. string dropString = "<select name=\"" + id + "\" id=\"" + id + "\" " + mHt.getCssClass(cssClass) + " " + mHt.getJavascript(javascript) + " size=\"5\">\n";
  156.  
  157. if (data != null)
  158. {
  159. for (int i = 1; i < data.GetUpperBound(0) + 1; ++i)
  160. {
  161. String curDesc = data[i, 0].ToString().Replace(" ", "&nbsp;");
  162. while (curDesc.IndexOf(" ") > -1)
  163. {
  164. curDesc = curDesc.Replace(" ", "&nbsp;");
  165. }
  166. dropString += "<option value=\"" + data[i, 0].ToString() + "\">" + curDesc + "</option>\n";
  167. }
  168. }
  169.  
  170. dropString += "</select>\n";
  171.  
  172. return dropString;
  173.  
  174. }
  175.  
  176. public static string button(string id, string value, string cssClass, string javascript)
  177. {
  178. HTML mHt = new HTML();
  179. return "<input type=\"button\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + value + "\" " + mHt.getCssClass(cssClass) + mHt.getJavascript(javascript) + "/>\n";
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. public string getCssClass(string cssClass)
  194. {
  195. string modCssClass = "";
  196. if (cssClass != "")
  197. {
  198. modCssClass = " class=\"" + cssClass + "\"";
  199. }
  200. return modCssClass;
  201. }
  202.  
  203. public string getJavascript(string javascript)
  204. {
  205. string modJavascript = "";
  206. if (javascript != "")
  207. {
  208. modJavascript = " " + javascript;
  209. }
  210. return modJavascript;
  211. }
  212.  
  213. public static string hidden(string id, string value, string cssClass, string javascript)
  214. {
  215. HTML mHt = new HTML();
  216. return "<input type=\"hidden\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + value + "\" " + mHt.getCssClass(cssClass) + mHt.getJavascript(javascript) + "/>\n";
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. public class cTable
  231. {
  232. public cTableRows Rows = new cTableRows();
  233.  
  234. private string tableBorder = "";
  235.  
  236. public void border(bool on)
  237. {
  238. if (on == true)
  239. {
  240. this.tableBorder = " border=\"1\" ";
  241. }
  242. else
  243. {
  244. this.tableBorder = "";
  245. }
  246. }
  247.  
  248. public string Text()
  249. {
  250. return "<table " + this.tableBorder + ">" + Rows.text() + "</table>";
  251. }
  252. }
  253.  
  254.  
  255. public class cTableRows
  256. {
  257. private Dictionary<string, string> myRows = new Dictionary<string, string>();
  258.  
  259. string rows = "";
  260. public void Add(cTableRow value)
  261. {
  262. myRows.Add(myRows.Count.ToString(), value.text());
  263. //"<td>" + value + "</td>\n";
  264. rows += "<tr>" + value.text() + "</tr>\n";
  265. }
  266.  
  267. public string text()
  268. {
  269. return rows;
  270. }
  271. }
  272.  
  273.  
  274.  
  275. public class cTableRow
  276. {
  277. public cRowCells Cells = new cRowCells();
  278.  
  279. public string text()
  280. {
  281. return Cells.text();
  282. }
  283. }
  284.  
  285. public class cRowCells
  286. {
  287. private Dictionary<string, string> myCells = new Dictionary<string, string>();
  288. private string cells = "";
  289. public void Add(cTableCell value)
  290. {
  291. myCells.Add(myCells.Count.ToString(), value.Text);
  292.  
  293. cells += "<td " + value.getSpan() + value.getRowSpan() + ">" + value.Text + "</td>\n";
  294. }
  295.  
  296. public string text()
  297. {
  298. return cells;
  299. }
  300.  
  301. }
  302.  
  303. public class cTableCell
  304. {
  305. public string Text;
  306.  
  307. public void rowSpan(int num)
  308. {
  309. cellRowSpan = " rowspan=\"" + num.ToString() + "\"";
  310. }
  311.  
  312. public void Span(int num)
  313. {
  314. cellSpan = " colspan=\"" + num.ToString() + "\" ";
  315. }
  316.  
  317. public string getSpan()
  318. {
  319. return cellSpan;
  320. }
  321.  
  322. public string getRowSpan()
  323. {
  324. return cellRowSpan;
  325. }
  326.  
  327. private string cellSpan = "";
  328. private string cellRowSpan = "";
  329. }
  330.  
  331. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.