protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["CurrentUser"] == null)
            {
                Response.Redirect("Logout.aspx");
            }

            string exportguid = Request.Form["exportguid"];
            if (string.IsNullOrEmpty(exportguid))
            {
                exportguid = Request.QueryString["exportguid"];
            }

            if (!string.IsNullOrEmpty(exportguid))
            {
                List<BestField> bparam = new List<BestField>();
                BestField fld = new BestField() { fieldName = "guidfield", fieldSize = 40, fieldType = "System.Guid", paramOledbType = System.Data.OleDb.OleDbType.Guid };
                fld.fieldValue = exportguid;
                bparam.Add(fld);
                BestExports bexp = new BestExports();
                bexp.LoadRows("guidfield=?", bparam);
                BestExportFields bexpFields = new BestExportFields();
                bexpFields.LoadRows("exportguid=?", bparam, "ordinal");
                string selfields = string.Join(",", bexpFields.TableRows.Rows.Select(x => x.Fields["fieldname"].fieldValue).ToArray());
                selfields = ReplaceT1(selfields);
                string headers = "\"" + string.Join("\",\"",bexpFields.TableRows.Rows.Select(x=>x.Fields["displayname"].fieldValue).ToArray()) + "\"\n";
                string filedata = getExportData(bexp.exportType, selfields);
                byte[] bytedata = strToByteArray(filedata);

                MemoryStream ms = new MemoryStream();
                byte[] bytes = strToByteArray(headers);
                ms.Write(bytes, 0, bytes.Length);
                ms.Write(bytedata, 0, bytedata.Length);
                string dest = bexp.exportType + DateTime.Today.ToString("MMddyyyy") + ".csv";

                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/csv";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + dest);
                Response.OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length);
                Response.Flush();
                Response.Close();
                ms.Close();
            }
        }
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["CurrentUser"] == null)
            {
                Response.Redirect("Logout.aspx");
            }
            ltrSubMenu.Text = UtilMenu.UtilityMenu("exportexcel");

            string ms = Request.QueryString["ms"];
            ltrMScript.Text = Utils.MenuSelectScript(ms);

            if (!Utils.User.UserRoleByName("Export To Excel").allowView)
            {
                ltrValidateMsg.Text = "You do not have rights to view.";
                return;
            }

            if (IsPostBack)
            {
                BestExports bs = new BestExports();
                string isnew = Request.Form["isnew"];
                bool cansave = true;
                Guid exportGuid = Guid.NewGuid();
                if (string.IsNullOrEmpty(isnew))
                {
                    List<BestField> bparams = new List<BestField>();
                    BestField guid = new BestField() { fieldName = "guidfield", fieldSize = 40, fieldType = "System.Guid", paramOledbType = System.Data.OleDb.OleDbType.Guid, displayField = false };
                    guid.fieldValue = Request.Form["guidfield"];
                    bparams.Add(guid);
                    if (Utils.IsGuid(guid.fieldValue))
                    {
                        exportGuid = new Guid(guid.fieldValue);
                    }

                    if (!string.IsNullOrEmpty(guid.fieldValue))
                    {
                        bs.LoadRows("guidfield=?", bparams);
                    }
                    else
                    {
                        string delguid = Request.Form["deleteguid"];
                        if (!string.IsNullOrEmpty(delguid))
                        {
                            bparams[0].fieldValue = delguid;
                            bs.LoadRows("guidfield=?", bparams);
                            bs.CurrentRow.IsDelete = true;
                            bs.CurrentRow.Save();
                        }
                        cansave = false;
                    }
                }
                else
                {
                    bs.guidfield = exportGuid;
                }

                if (cansave)
                {
                    if (!string.IsNullOrEmpty(isnew)) { bs.exportName = Request.Form["g_exportName"]; }
                    if (string.IsNullOrEmpty(bs.exportName))
                    {
                        ltrValidateMsg.Text = Utils.WarningMessage("Export Name is Required.");
                        cansave = false;
                    }
                    if (cansave)
                    {
                        bs.exportType = Request.Form["g_exportType"];
                        bs.exportTitle = Request.Form["g_exportTitle"];
                        if (!bs.CurrentRow.Save())
                        {
                            ltrValidateMsg.Text = Utils.WarningMessage(bs.CurrentRow.lastError);
                        }
                        else
                        {
                            /* Insert or Update Export Fields */
                            string FieldData = Request.Form["hidExportData"] ?? "";
                            if (!string.IsNullOrEmpty(FieldData))
                            {
                                string[] lines = FieldData.Split('\n');
                                int i = 0;
                                foreach (string line in lines)
                                {
                                    if (!string.IsNullOrEmpty(line))
                                    {
                                        string[] fields = line.Replace("\r","").Split('\t');
                                        //0.guidfield 1.exportguid 2.fieldname 3.displayname 4.isnew 5.isdeleted
                                        if (fields[4].Equals("1"))
                                        {
                                            if (fields[5].Equals("0") && (!string.IsNullOrEmpty(fields[2])))
                                            {
                                                /* Add New */
                                                BestExportFields bexpfld = new BestExportFields();
                                                bexpfld.exportguid = exportGuid;
                                                bexpfld.fieldName = fields[2];
                                                bexpfld.displayName = fields[3];
                                                int idx;
                                                int.TryParse(fields[6], out idx);
                                                bexpfld.Ordinal = idx;
                                                if (!bexpfld.CurrentRow.Save())
                                                {
                                                    ltrValidateMsg.Text = Utils.WarningMessage(bs.CurrentRow.lastError);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            /* Update */
                                            List<BestField> bparam = new List<BestField>();
                                            BestField fguid = new BestField() { fieldName = "guidfield", fieldSize = 40, fieldType = "System.Guid", paramOledbType = System.Data.OleDb.OleDbType.Guid, displayField = false };
                                            fguid.fieldValue = fields[0];
                                            bparam.Add(fguid);
                                            BestExportFields bsFields = new BestExportFields();
                                            bsFields.LoadRows("guidfield=?", bparam);
                                            bsFields.fieldName = fields[2];
                                            bsFields.displayName = fields[3];
                                            int idx;
                                            int.TryParse(fields[6], out idx);
                                            bsFields.Ordinal = idx;
                                            bsFields.CurrentRow.IsDelete = fields[5].Equals("1");
                                            if (!bsFields.CurrentRow.Save())
                                            {
                                                ltrValidateMsg.Text = Utils.WarningMessage(bs.CurrentRow.lastError);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if ((Request.Form["SaveAndExport"] ?? "").Equals("1"))
                {
                    ltrExportScript.Text = @"<script type=""text/javascript"">$(document).ready(function(){
                            $('#addiframe').html('<iframe src = ""DownloadExport.aspx?exportguid=" + exportGuid + @""" /></iframe>');
                    });</script>";
                }
            }

            ltrPageScript.Text = "<script type=\"text/javascript\">" + ExportFieldJS() +"\n" + ExportMastTableJs() + "</script>";

            BestGrid bsGrid = new BestGrid();
            bsGrid.PageRequest = Page.Request;
            bsGrid.Title = "Export Information";
            bsGrid.securityPage = "Export To Excel";
            bsGrid.GridTable = new BestExports();
            bsGrid.extraRowHTML = "<a href=\"DownloadExport.aspx?exportguid=[paramGuid]\" ><img src=\"images\\xls.gif\" alt=\"runexport\" /></a>";
            ltrGrid.Text = bsGrid.ToHTML();
        }