using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Admin_ContactFormSubmissions : WebBlocks.AdministrativePage { public int ContactFormID { get { return (int)ViewState["ContactFormID"]; } set { ViewState["ContactFormID"] = value; } } public int PageIndex { get { return (ViewState["PageIndex"] == null ? 1 :(int)ViewState["PageIndex"]); } set { ViewState["PageIndex"] = value; } } public int PageSize { get { return (ViewState["PageSize"] == null ? 20 : (int)ViewState["PageSize"]); } set { ViewState["PageSize"] = value; } } public int TotalResults { get { return (ViewState["TotalResults"] == null ? 0 : (int)ViewState["TotalResults"]); } set { ViewState["TotalResults"] = value; } } public int TotalPages { get { return (int)Math.Ceiling((double)TotalResults / PageSize); } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request["ContactFormID"] != null) ContactFormID = Int32.Parse(Request["ContactFormID"]); if (Request["PageIndex"] != null) PageIndex = Int32.Parse(Request["PageIndex"]); if (Request["PageSize"] != null) PageSize = Int32.Parse(Request["PageSize"]); BindData(); BindSubmissions(); } } private void BindData() { DataRow ContactFormRow = Database.ContactForm_GetByContactFormID(ContactFormID); litBlockTitle.Text = (string)ContactFormRow["BlockTitle"]; TotalResults = Database.ContactFormSubmissions_GetCount(ContactFormID); } private void BindSubmissions() { DataTable dt = Database.ContactFormSubmissions_GetPaged(ContactFormID, PageIndex, PageSize); gvSubmissions.DataSource = dt; gvSubmissions.DataBind(); lbBack.Visible = (PageIndex > 1); lbNext.Visible = (PageIndex < TotalPages); litPagination.Text = PageIndex.ToString() + " of " + TotalPages.ToString(); } protected void btnApply_Click(object sender, EventArgs e) { PageSize = Int32.Parse(ddlPageSize.SelectedValue); PageIndex = 1; BindSubmissions(); } protected void lbBack_Click(object sender, EventArgs e) { PageIndex--; BindSubmissions(); } protected void lbNext_Click(object sender, EventArgs e) { PageIndex++; BindSubmissions(); } protected void btnExport_Click(object sender, EventArgs e) { DataTable dt; if (ddlExport.SelectedValue == "1") dt = Database.ContactFormSubmissions_ExportPage(ContactFormID, PageIndex, PageSize); else dt = Database.ContactFormSubmissions_ExportAll(ContactFormID); Response.Clear(); Response.Buffer = true; Response.ContentType = "text/csv"; Response.AddHeader("Content-Disposition", "inline;filename=Submissions.csv"); Response.Charset = ""; this.EnableViewState = false; ProduceCSV(dt, Response.Output, true); Response.End(); } #region export CSV public static void ProduceCSV(DataTable dt, System.IO.TextWriter httpStream, bool WriteHeader) { if (WriteHeader) { string[] arr = new String[dt.Columns.Count]; for (int i = 0; i < dt.Columns.Count; i++) { arr[i] = dt.Columns[i].ColumnName; arr[i] = GetWriteableValue(arr[i]); } httpStream.WriteLine(string.Join(",", arr)); } for (int j = 0; j < dt.Rows.Count; j++) { string[] dataArr = new String[dt.Columns.Count]; for (int i = 0; i < dt.Columns.Count; i++) { object o = dt.Rows[j][i]; dataArr[i] = GetWriteableValue(o); } httpStream.WriteLine(string.Join(",", dataArr)); } } public static string GetWriteableValue(object o) { if (o == null || o == Convert.DBNull) return ""; else if (o.ToString().IndexOf(",") == -1) return o.ToString(); else return "\"" + o.ToString() + "\""; } #endregion protected void gvSubmissions_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "detail") { int ContactFormSubmissionID = (int)gvSubmissions.DataKeys[Int32.Parse(e.CommandArgument.ToString())].Value; pnlDetail.Visible = true; DataTable dt = Database.ContactFormSubmissionData_Get(ContactFormSubmissionID); gvDetail.DataSource = dt; gvDetail.DataBind(); } } }