PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/BaliEnterpriseSystems/BaliEnterpriseSystems/BestExportToExcel.aspx.cs

https://github.com/sirivedula/BEST
C# | 183 lines | 170 code | 9 blank | 4 comment | 21 complexity | f9158302e48ced2fefbdd14bfd27e9ab MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using BaliEnterpriseSystems.BestObjects;
  8. using System.Text;
  9. namespace BaliEnterpriseSystems
  10. {
  11. public partial class BestExportToExcel : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. }
  16. protected void Page_LoadComplete(object sender, EventArgs e)
  17. {
  18. if (HttpContext.Current.Session["CurrentUser"] == null)
  19. {
  20. Response.Redirect("Logout.aspx");
  21. }
  22. ltrSubMenu.Text = UtilMenu.UtilityMenu("exportexcel");
  23. string ms = Request.QueryString["ms"];
  24. ltrMScript.Text = Utils.MenuSelectScript(ms);
  25. if (!Utils.User.UserRoleByName("Export To Excel").allowView)
  26. {
  27. ltrValidateMsg.Text = "You do not have rights to view.";
  28. return;
  29. }
  30. if (IsPostBack)
  31. {
  32. BestExports bs = new BestExports();
  33. string isnew = Request.Form["isnew"];
  34. bool cansave = true;
  35. Guid exportGuid = Guid.NewGuid();
  36. if (string.IsNullOrEmpty(isnew))
  37. {
  38. List<BestField> bparams = new List<BestField>();
  39. BestField guid = new BestField() { fieldName = "guidfield", fieldSize = 40, fieldType = "System.Guid", paramOledbType = System.Data.OleDb.OleDbType.Guid, displayField = false };
  40. guid.fieldValue = Request.Form["guidfield"];
  41. bparams.Add(guid);
  42. if (Utils.IsGuid(guid.fieldValue))
  43. {
  44. exportGuid = new Guid(guid.fieldValue);
  45. }
  46. if (!string.IsNullOrEmpty(guid.fieldValue))
  47. {
  48. bs.LoadRows("guidfield=?", bparams);
  49. }
  50. else
  51. {
  52. string delguid = Request.Form["deleteguid"];
  53. if (!string.IsNullOrEmpty(delguid))
  54. {
  55. bparams[0].fieldValue = delguid;
  56. bs.LoadRows("guidfield=?", bparams);
  57. bs.CurrentRow.IsDelete = true;
  58. bs.CurrentRow.Save();
  59. }
  60. cansave = false;
  61. }
  62. }
  63. else
  64. {
  65. bs.guidfield = exportGuid;
  66. }
  67. if (cansave)
  68. {
  69. if (!string.IsNullOrEmpty(isnew)) { bs.exportName = Request.Form["g_exportName"]; }
  70. if (string.IsNullOrEmpty(bs.exportName))
  71. {
  72. ltrValidateMsg.Text = Utils.WarningMessage("Export Name is Required.");
  73. cansave = false;
  74. }
  75. if (cansave)
  76. {
  77. bs.exportType = Request.Form["g_exportType"];
  78. bs.exportTitle = Request.Form["g_exportTitle"];
  79. if (!bs.CurrentRow.Save())
  80. {
  81. ltrValidateMsg.Text = Utils.WarningMessage(bs.CurrentRow.lastError);
  82. }
  83. else
  84. {
  85. /* Insert or Update Export Fields */
  86. string FieldData = Request.Form["hidExportData"] ?? "";
  87. if (!string.IsNullOrEmpty(FieldData))
  88. {
  89. string[] lines = FieldData.Split('\n');
  90. int i = 0;
  91. foreach (string line in lines)
  92. {
  93. if (!string.IsNullOrEmpty(line))
  94. {
  95. string[] fields = line.Replace("\r","").Split('\t');
  96. //0.guidfield 1.exportguid 2.fieldname 3.displayname 4.isnew 5.isdeleted
  97. if (fields[4].Equals("1"))
  98. {
  99. if (fields[5].Equals("0") && (!string.IsNullOrEmpty(fields[2])))
  100. {
  101. /* Add New */
  102. BestExportFields bexpfld = new BestExportFields();
  103. bexpfld.exportguid = exportGuid;
  104. bexpfld.fieldName = fields[2];
  105. bexpfld.displayName = fields[3];
  106. int idx;
  107. int.TryParse(fields[6], out idx);
  108. bexpfld.Ordinal = idx;
  109. if (!bexpfld.CurrentRow.Save())
  110. {
  111. ltrValidateMsg.Text = Utils.WarningMessage(bs.CurrentRow.lastError);
  112. }
  113. }
  114. }
  115. else
  116. {
  117. /* Update */
  118. List<BestField> bparam = new List<BestField>();
  119. BestField fguid = new BestField() { fieldName = "guidfield", fieldSize = 40, fieldType = "System.Guid", paramOledbType = System.Data.OleDb.OleDbType.Guid, displayField = false };
  120. fguid.fieldValue = fields[0];
  121. bparam.Add(fguid);
  122. BestExportFields bsFields = new BestExportFields();
  123. bsFields.LoadRows("guidfield=?", bparam);
  124. bsFields.fieldName = fields[2];
  125. bsFields.displayName = fields[3];
  126. int idx;
  127. int.TryParse(fields[6], out idx);
  128. bsFields.Ordinal = idx;
  129. bsFields.CurrentRow.IsDelete = fields[5].Equals("1");
  130. if (!bsFields.CurrentRow.Save())
  131. {
  132. ltrValidateMsg.Text = Utils.WarningMessage(bs.CurrentRow.lastError);
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. if ((Request.Form["SaveAndExport"] ?? "").Equals("1"))
  142. {
  143. ltrExportScript.Text = @"<script type=""text/javascript"">$(document).ready(function(){
  144. $('#addiframe').html('<iframe src = ""DownloadExport.aspx?exportguid=" + exportGuid + @""" /></iframe>');
  145. });</script>";
  146. }
  147. }
  148. ltrPageScript.Text = "<script type=\"text/javascript\">" + ExportFieldJS() +"\n" + ExportMastTableJs() + "</script>";
  149. BestGrid bsGrid = new BestGrid();
  150. bsGrid.PageRequest = Page.Request;
  151. bsGrid.Title = "Export Information";
  152. bsGrid.securityPage = "Export To Excel";
  153. bsGrid.GridTable = new BestExports();
  154. bsGrid.extraRowHTML = "<a href=\"DownloadExport.aspx?exportguid=[paramGuid]\" ><img src=\"images\\xls.gif\" alt=\"runexport\" /></a>";
  155. ltrGrid.Text = bsGrid.ToHTML();
  156. }
  157. private string ExportFieldJS()
  158. {
  159. BestExportFields bexpFlds = new BestExportFields();
  160. bexpFlds.LoadRows("exportguid,ordinal");
  161. return "jsExpFields = [" + string.Join(",", bexpFlds.TableRows.Rows.Select(x => "{guidfield:" + Utils.EnquoteJS(x.Fields["guidfield"].fieldValue) + ",exportguid:" + Utils.EnquoteJS(x.Fields["exportguid"].fieldValue) + ",fieldname:" + Utils.EnquoteJS(x.Fields["fieldname"].fieldValue) + ",displayname:" + Utils.EnquoteJS(x.Fields["displayname"].fieldValue) + ",isnew:0,isdeleted:0,Index:\"" + x.Fields["ordinal"].fieldValue + "\"}").ToArray()) + "]";
  162. }
  163. private string ExportMastTableJs()
  164. {
  165. BestExportTables bexpTbl = new BestExportTables();
  166. bexpTbl.LoadRows("displayname");
  167. return "var selHTML = [" + string.Join(",", bexpTbl.TableRows.Rows.Select(x => "{fieldname:" + Utils.EnquoteJS(x.Fields["fieldname"].fieldValue) + ",displayname:" + Utils.EnquoteJS(x.Fields["displayname"].fieldValue) + ",aliasname:" + Utils.EnquoteJS(x.Fields["aliasname"].fieldValue) + ",exportType:" + Utils.EnquoteJS(x.Fields["exporttype"].fieldValue) + "}").ToArray()) + "]";
  168. }
  169. }
  170. }