PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/BaliEnterpriseSystems/BaliEnterpriseSystems/BestObjects/BestGrid.cs

https://github.com/sirivedula/BEST
C# | 505 lines | 450 code | 39 blank | 16 comment | 57 complexity | 923ff048e728e862ad5abd5697dd1772 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.Data.OleDb;
  15. using System.Text;
  16. namespace BaliEnterpriseSystems.BestObjects
  17. {
  18. public class BestGrid
  19. {
  20. public BestGrid()
  21. {
  22. this.whereClause = "";
  23. this.allowAdd = true;
  24. this.allowDelete = true;
  25. this.allowEdit = true;
  26. }
  27. public string orderby
  28. {
  29. get;
  30. set;
  31. }
  32. /* Grid To HTML */
  33. public string ToHTML()
  34. {
  35. StringBuilder gridhtml = new StringBuilder();
  36. gridhtml.Append("<div class=\"centered\">");
  37. gridhtml.Append("<table class=\"gridtitle\"><tr><td>" + HttpUtility.HtmlEncode(this.Title) + "</td></tr></table>");
  38. gridhtml.Append("<table id=\"tblGrid\" class=\"bestgrid\" style=\"width:100%;\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
  39. gridhtml.Append(SearchAndPageHTML());
  40. gridhtml.Append(HeaderHTML);
  41. gridhtml.Append(MakeRowsHTML);
  42. gridhtml.Append("</table>");
  43. if (GridTable.allowAdd && this.allowAdd)
  44. {
  45. gridhtml.Append("<span><input type=\"button\" id=\"btnAdd\" value=\"Add\" onclick=\"addNew();\" /></span>");
  46. }
  47. if (!string.IsNullOrEmpty(this.AfterAddHTML))
  48. {
  49. gridhtml.Append(this.AfterAddHTML);
  50. }
  51. gridhtml.Append("</div>");
  52. gridhtml.Append("<span><input type=\"hidden\" id=\"isnew\" name=\"isnew\" /><input type=\"hidden\" id=\"guidfield\" name=\"guidfield\" /><input type=\"hidden\" id=\"deleteguid\" name=\"deleteguid\" /></span>");
  53. gridhtml.Append(HiddenFields());
  54. /* Grid Java Script */
  55. gridhtml.Append(gridScript());
  56. return gridhtml.ToString();
  57. }
  58. /* Grid Table */
  59. public BestTable GridTable
  60. {
  61. get;
  62. set;
  63. }
  64. /* 1. Title of the Grid */
  65. public string Title
  66. {
  67. get;
  68. set;
  69. }
  70. public HttpRequest PageRequest
  71. {
  72. get;
  73. set;
  74. }
  75. private int startRec
  76. {
  77. get;
  78. set;
  79. }
  80. private int endRec
  81. {
  82. get;
  83. set;
  84. }
  85. private int TotalRec
  86. {
  87. get
  88. {
  89. return GridTable.TableRows.Count;
  90. }
  91. }
  92. private string perPage
  93. {
  94. get;
  95. set;
  96. }
  97. private string HeaderSortBy
  98. {
  99. get;
  100. set;
  101. }
  102. private string HeaderSortDir
  103. {
  104. get;
  105. set;
  106. }
  107. public string whereClause
  108. {
  109. get;
  110. set;
  111. }
  112. private List<BestField> _whereParams;
  113. public List<BestField> whereParams
  114. {
  115. get
  116. {
  117. return _whereParams;
  118. }
  119. set
  120. {
  121. _whereParams = value;
  122. }
  123. }
  124. private void filtertheSearch()
  125. {
  126. string inputstr = PageRequest.Form["searchValue"];
  127. string searchField = PageRequest.Form["searchField"];
  128. this.HeaderSortDir = PageRequest.Form["sortdir"];
  129. this.HeaderSortBy = PageRequest.Form["sortby"];
  130. if (string.IsNullOrEmpty(this.HeaderSortBy))
  131. {
  132. for (int cnt = 0; cnt < this.GridTable.TableFields.Count; cnt++ )
  133. {
  134. if (this.GridTable.TableFields[cnt].displayField)
  135. {
  136. this.HeaderSortBy = this.GridTable.TableFields[cnt].fieldName;
  137. break;
  138. }
  139. }
  140. }
  141. if (string.IsNullOrEmpty(this.HeaderSortDir))
  142. {
  143. this.HeaderSortDir = "asc";
  144. }
  145. if (!string.IsNullOrEmpty(inputstr) && !string.IsNullOrEmpty(searchField))
  146. {
  147. List<BestField> listbfld = new List<BestField>();
  148. if (!string.IsNullOrEmpty(this.whereClause))
  149. {
  150. searchField = this.whereClause + " and " + searchField;
  151. for (int i = 0; i < this.whereParams.Count; i++)
  152. {
  153. listbfld.Add(this.whereParams[i]);
  154. }
  155. }
  156. BestField bfld = new BestField() { fieldName = searchField, displayField = false, fieldType = "System.String", paramOledbType = OleDbType.VarChar, fieldSize = 20 };
  157. bfld.fieldValue = inputstr + '%';
  158. listbfld.Add(bfld);
  159. if (this.hasCenterId())
  160. {
  161. listbfld.Add(Utils.User.CenterIdField);
  162. this.GridTable.LoadRows(searchField + " like ? and CenterId=?", listbfld, this.HeaderSortBy + " " + this.HeaderSortDir);
  163. }
  164. else
  165. {
  166. this.GridTable.LoadRows(searchField + " like ?", listbfld, this.HeaderSortBy + " " + this.HeaderSortDir);
  167. }
  168. }
  169. else
  170. {
  171. if (!string.IsNullOrEmpty(this.whereClause))
  172. {
  173. if (hasCenterId())
  174. {
  175. _whereParams.Add(Utils.User.CenterIdField);
  176. this.GridTable.LoadRows(this.whereClause + " and CenterId=?", this.whereParams, this.HeaderSortBy + " " + this.HeaderSortDir);
  177. }
  178. else
  179. {
  180. this.GridTable.LoadRows(this.whereClause, this.whereParams, this.HeaderSortBy + " " + this.HeaderSortDir);
  181. }
  182. }
  183. else
  184. {
  185. if (hasCenterId())
  186. {
  187. this.GridTable.LoadRows("CenterId=?",Utils.User.CIdParam, this.HeaderSortBy + " " + this.HeaderSortDir);
  188. }
  189. else
  190. {
  191. this.GridTable.LoadRows(this.HeaderSortBy + " " + this.HeaderSortDir);
  192. }
  193. }
  194. }
  195. }
  196. private bool hasCenterId()
  197. {
  198. bool result = false;
  199. if (!(this.GridTable is BestCenters))
  200. {
  201. for (int i = 0; i < GridTable.TableFields.Count; i++)
  202. {
  203. BestField bfld = GridTable.TableFields[i];
  204. if (bfld.fieldName.Equals("CenterId", StringComparison.InvariantCultureIgnoreCase))
  205. {
  206. result = true;
  207. break;
  208. }
  209. }
  210. }
  211. return result;
  212. }
  213. /* 2. Search - Per Page - Page List */
  214. private string SearchAndPageHTML()
  215. {
  216. filtertheSearch();
  217. StringBuilder sb = new StringBuilder();
  218. this.perPage = PageRequest.Form["perpage"];
  219. UserPrefs up = new UserPrefs(Utils.User.UserName);
  220. string prefPerPage = up.getPreference(this.GridTable.TableName);
  221. if (string.IsNullOrEmpty(this.perPage))
  222. {
  223. this.perPage = string.IsNullOrEmpty(prefPerPage) ? "50" : prefPerPage;
  224. }
  225. if (! prefPerPage.Equals(this.perPage))
  226. {
  227. up.SetPreference(this.GridTable.TableName, this.perPage);
  228. }
  229. string[] aryPerPage = new string[] { "10", "20", "30", "40", "50", "70", "100" };
  230. sb.Append("<thead><tr><td colspan=\"" + (GridTable.TableFields.Count + 1).ToString() + "\">");
  231. sb.Append("<table class=\"gridpaging\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td>Per Page</td><td>");
  232. sb.Append("<input type=\"hidden\" id=\"sortby\" name=\"sortby\" value=\"" + HttpUtility.HtmlEncode(this.HeaderSortBy) + "\" /><input type=\"hidden\" id=\"sortdir\" name=\"sortdir\" value=\"" + HttpUtility.HtmlEncode(this.HeaderSortDir) + "\"/>");
  233. sb.Append("<select id=\"perpage\" name=\"perpage\" onchange=\"submit();\">");
  234. for (int i = 0; i < aryPerPage.Length; i++)
  235. {
  236. sb.Append("<option value=\"" + HttpUtility.HtmlEncode(aryPerPage[i]) + "\" " + (this.perPage.Equals(aryPerPage[i]) ? "selected" : "") + " >" + HttpUtility.HtmlEncode(aryPerPage[i]) + "</option>");
  237. }
  238. sb.Append("</select></td><td style=\"border-right:1px solid #333;\">&nbsp;</td>");
  239. sb.Append("<td>Search</td><td><select style=\"padding:2px;font-family:Tahoma;font-size:10px;\" id=\"searchField\" name=\"searchField\" ><option value=\"\"></option>");
  240. string searchField = PageRequest.Form["searchField"];
  241. for(int j=0; j< GridTable.TableFields.Count; j++)
  242. {
  243. string fldname = GridTable.TableFields[j].fieldName.ToLower();
  244. if (!(fldname.Equals("guidfield") || fldname.Equals("lastchange") || fldname.Equals("lastchangeuser")))
  245. {
  246. sb.Append("<option value=\"" + HttpUtility.HtmlEncode(fldname) + "\" " + ((!string.IsNullOrEmpty(searchField) && searchField.Equals(fldname, StringComparison.InvariantCultureIgnoreCase))? " selected " : "") + ">" + HttpUtility.HtmlEncode( GridTable.TableFields[j].fieldHeader) + "</option>");
  247. }
  248. }
  249. string searchVal = PageRequest.Form["searchValue"];
  250. if (string.IsNullOrEmpty(searchVal)) searchVal = "";
  251. sb.Append("</select></td><td><input type=\"text\" style=\"padding:2px;font-family:Tahoma;font-size:10px;\" id=\"searchValue\" name=\"searchValue\" size=\"12\" maxlength=\"20\" value=\"" + HttpUtility.HtmlEncode(searchVal) + "\" /></td><td><a href=\"javascript:void(0);\" onclick=\"submit();\"><img src=\"images\\zoom.png\" style=\"height:16px;width:16px;vertical-align:middle;border:0px;\" alt=\"search\" /></a></td>");
  252. /* Pages HTML */
  253. int perpage;
  254. int.TryParse(this.perPage, out perpage);
  255. this.startRec = 0;
  256. this.endRec = this.TotalRec;
  257. if (this.TotalRec > perpage)
  258. {
  259. sb.Append("<td style=\"border-right:1px solid #333;\">&nbsp;</td>");
  260. int numPages = (this.TotalRec / perpage) + 1;
  261. string cpage = PageRequest.Form["curpage"];
  262. int curpage;
  263. int.TryParse(cpage, out curpage);
  264. if (curpage > 1)
  265. {
  266. sb.Append("<td><img src=\"images\\pagestart.png\" alt=\"start\" title=\"start page\" onclick=\"gotoPage(0);\" /></td>");
  267. sb.Append("<td><img src=\"images\\pageprior.png\" alt=\"prior\" title=\"prior page\" onclick=\"gotoPage(-1);\" /></td>");
  268. }
  269. sb.Append("<td><select id=\"curpage\" name=\"curpage\" onchange=\"submit();\">");
  270. for (int pn = 1; pn <= numPages; pn++)
  271. {
  272. sb.Append("<option value=\"" + pn.ToString() + "\" " + ((curpage==pn)?"selected":"") + " >" + pn.ToString() + "</option>");
  273. }
  274. sb.Append("</select></td>");
  275. if (curpage != numPages)
  276. {
  277. sb.Append("<td><img src=\"images\\pagenext.png\" alt=\"next\" title=\"next page\" onclick=\"gotoPage(1);\" /></td>");
  278. sb.Append("<td><img src=\"images\\pageend.png\" alt=\"end\" title=\"end page\" onclick=\"gotoPage(" + numPages.ToString() + ");\" /></td>");
  279. }
  280. this.startRec = (curpage!=0) ? (curpage-1) * perpage : 0;
  281. this.endRec = this.startRec + perpage;
  282. if (this.endRec > this.TotalRec) this.endRec = this.TotalRec;
  283. }
  284. sb.Append("</tr></table>");
  285. sb.Append("</td></tr></thead>");
  286. return sb.ToString();
  287. }
  288. /* 3. Grid Headers */
  289. private string HeaderHTML
  290. {
  291. get
  292. {
  293. StringBuilder sb = new StringBuilder();
  294. sb.Append("<thead class=\"gridheader\"><tr>");
  295. for (int i = 0; i < GridTable.TableFields.Count; i++)
  296. {
  297. BestField bfld = GridTable.TableFields[i];
  298. if (bfld.displayField)
  299. {
  300. sb.Append("<th onclick=\"BestJSGrid.HeaderSort('" + bfld.fieldName +"');\">" + HttpUtility.HtmlEncode(bfld.fieldHeader));
  301. if (this.HeaderSortBy.Equals(bfld.fieldName))
  302. {
  303. if (this.HeaderSortDir.Equals("asc"))
  304. {
  305. sb.Append("<span style=\"padding-left:5px;\"><img src=\"images/ARW03DN.png\" alt=\"\" /></span>");
  306. }
  307. else
  308. {
  309. sb.Append("<span style=\"padding-left:5px;\"><img src=\"images/ARW03UP.png\" alt=\"\" /></span>");
  310. }
  311. }
  312. sb.Append("</th>");
  313. }
  314. }
  315. sb.Append("<th>&nbsp;</th>");
  316. if (!string.IsNullOrEmpty(this.extraRowHTML))
  317. {
  318. sb.Append("<th>&nbsp;</th>");
  319. }
  320. sb.Append("</tr></thead>");
  321. return sb.ToString();
  322. }
  323. }
  324. /* Make Rows */
  325. private string MakeRowsHTML
  326. {
  327. get
  328. {
  329. StringBuilder rowshtml = new StringBuilder();
  330. for (int r = this.startRec; r < this.endRec; r++)
  331. {
  332. string srclass = " class=" + ((r % 2 == 0) ? "\"grideven\"" : "\"gridodd\"");
  333. rowshtml.Append(RowHTML(r, GridTable.TableRows[r], srclass));
  334. }
  335. return rowshtml.ToString();
  336. }
  337. }
  338. /* 4. Make Data Rows */
  339. private string RowHTML(int rownum, BestRow brow, string srclass)
  340. {
  341. StringBuilder sb = new StringBuilder();
  342. sb.Append("<tr" + srclass + ">");
  343. for (int i = 0; i < brow.Fields.Count; i++)
  344. {
  345. if (brow.Fields[i].displayField)
  346. {
  347. sb.Append("<td>");
  348. if (brow.Fields[i].displayFormatFunc != null)
  349. {
  350. sb.Append(brow.Fields[i].displayFormatFunc.Invoke(brow));
  351. }
  352. else
  353. {
  354. sb.Append(HttpUtility.HtmlEncode(brow.Fields[i].fieldValue));
  355. }
  356. string htmlname = "Field" + rownum.ToString() + "_" + i.ToString();
  357. /*
  358. * sb.Append("<input type=\"hidden\" id=\"" + htmlname + "\" name=\"" + htmlname + "\" value=\"" + HttpUtility.HtmlEncode(brow.Fields[i].fieldValue) + "\" />");
  359. */
  360. sb.Append("</td>" + '\n');
  361. }
  362. }
  363. sb.Append("<td>");
  364. if (GridTable.allowEdit && this.allowEdit)
  365. {
  366. sb.Append("<a href=\"javascript:void(0);\" onclick=\"editForm('" + rownum.ToString() + "');\"><img alt=\"edit\" style=\"border:0px;\" src=\"images/gridedit.png\" title=\"edit the record\" /></a>&nbsp;&nbsp;");
  367. }
  368. if (GridTable.allowDelete && this.allowDelete)
  369. {
  370. sb.Append("<a href=\"javascript:void(0);\" onclick=\"BestJSGrid.deleteRec('" + brow.Fields["guidfield"].fieldValue + "');\"><img src=\"images/cancel.png\" style=\"border:0px;\" alt=\"delete\" title=\"delete the record\" /></a></td>");
  371. }
  372. if (!string.IsNullOrEmpty(this.extraRowHTML))
  373. {
  374. string replExtraHTML = extraRowHTML.Replace("[paramGuid]", HttpUtility.UrlEncode(brow.Fields["guidfield"].fieldValue));
  375. replExtraHTML = replExtraHTML.Replace("[paramCID]", HttpUtility.UrlEncode(brow.Fields["CenterId"].fieldValue));
  376. replExtraHTML = replExtraHTML.Replace("[paramSID]", HttpUtility.UrlEncode(brow.Fields["StudentId"].fieldValue));
  377. replExtraHTML = replExtraHTML.Replace("[paramRowNum]", HttpUtility.UrlEncode(rownum.ToString()));
  378. sb.Append("<td>" + replExtraHTML + "</td>");
  379. }
  380. sb.Append("</tr>");
  381. return sb.ToString();
  382. }
  383. /* 5. New Hidden Row
  384. * Will open new page and save it. Even Update will also go to the new page and save it.
  385. * Do not need to be on the same page - make it simple.
  386. */
  387. private string gridScript()
  388. {
  389. StringBuilder sb = new StringBuilder();
  390. sb.AppendLine("<script type=\"text/javascript\">");
  391. sb.AppendLine("gridJS = {title:" + Utils.EnquoteJS(this.Title) + ",");
  392. sb.AppendLine("Rows:[");
  393. for (int r = 0; r < GridTable.TableRows.Count; r++)
  394. {
  395. BestRow brow = GridTable.TableRows[r];
  396. sb.Append("{");
  397. for (int c = 0; c < brow.Fields.Count; c++)
  398. {
  399. if (brow.Fields[c].paramOledbType == OleDbType.DBDate)
  400. {
  401. if (!string.IsNullOrEmpty(brow.Fields[c].fieldValue))
  402. {
  403. DateTime dd = Convert.ToDateTime(brow.Fields[c].fieldValue);
  404. sb.Append(Utils.EnquoteJS(brow.Fields[c].fieldName) + ":" + Utils.EnquoteJS(dd.ToString("MM/dd/yyyy")));
  405. }
  406. else
  407. {
  408. sb.Append(Utils.EnquoteJS(brow.Fields[c].fieldName) + ":" + "\"\"");
  409. }
  410. }
  411. else
  412. {
  413. sb.Append(Utils.EnquoteJS(brow.Fields[c].fieldName) + ":" + Utils.EnquoteJS(brow.Fields[c].fieldValue));
  414. }
  415. if (c < brow.Fields.Count - 1) sb.Append(",");
  416. }
  417. sb.AppendLine("}");
  418. if (r < GridTable.TableRows.Count - 1) sb.Append(",");
  419. }
  420. sb.Append("]}");
  421. sb.Append("</script>");
  422. return sb.ToString();
  423. }
  424. private string HiddenFields()
  425. {
  426. StringBuilder sb = new StringBuilder();
  427. sb.Append("<span>");
  428. for (int c = 0; c < GridTable.TableFields.Count; c++)
  429. {
  430. sb.Append("<input type=\"hidden\" id=\"g_" + GridTable.TableFields[c].fieldName + "\" name=\"g_" + GridTable.TableFields[c].fieldName + "\" />");
  431. }
  432. sb.Append("</span>");
  433. return sb.ToString();
  434. }
  435. public string securityPage
  436. {
  437. get;
  438. set;
  439. }
  440. public string AfterAddHTML
  441. {
  442. get;
  443. set;
  444. }
  445. public string extraRowHTML
  446. {
  447. get;
  448. set;
  449. }
  450. public bool allowAdd
  451. {
  452. get;
  453. set;
  454. }
  455. public bool allowEdit
  456. {
  457. get;
  458. set;
  459. }
  460. public bool allowDelete
  461. {
  462. get;
  463. set;
  464. }
  465. }
  466. }