Hi,
I've really hit a wall here, any help would be really appreciated:
i can't seem to retain a dynamic check box list when clicking a button in the UserControl form. i have tried all the possible ' if (!IsPostBack) ' methods, but every time i click the download button or i change the date (using ajaxToolkit:CalendarExtender) the page reloads and i lose the dynamic check box list .it could be related to the Ajax but im not sure.
this is my DownloadForm.ascx:
<%@ Control Language="C#" AutoEventWireup="false" CodeBehind="DownloadForm.ascx.cs" Inherits="phytech.Web.UI.DownloadFormModule" %>
Dates
Choose different Dates
Start Date:
(Set the focus to the textbox to show the calendar)
End Date:
|
|
And this is the codebehind :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Net;
using System.IO;
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;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using log4net;
//using phytech.Web.Business;
using phytech.Business;
using Resources;
using AjaxControlToolkit;
//using phytech.Web.UI.App_GlobalResources;
using Ionic.Zip;
namespace phytech.Web.UI
{
public partial class DownloadFormModule : SiteModuleControl
{
private DownloadFormRepository repository = new DownloadFormRepository();
#region OnInit
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new EventHandler(Page_Load);
DownloadButton.Click += new EventHandler(DownloadButton_Click);
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
LoadSettings();
PopulateLabels();
PopulateControls();
chkButton.Attributes.Add("onclick", "return fnCheckAll();");
clrButton.Attributes.Add("onclick", "return fnClearAll();");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
FillDates();
}
private void PopulateControls()
{
// TitleControl.EditUrl = SiteRoot + "/DownloadForm/DownloadForm.aspx";
TitleControl.Visible = !this.RenderInWebPartMode;
if (this.ModuleConfiguration != null)
{
this.Title = this.ModuleConfiguration.ModuleTitle;
this.Description = this.ModuleConfiguration.FeatureName;
}
}
private void PopulateLabels()
{
TitleControl.EditText = "Edit";
DownloadButton.Text = DownloadFormResources.DownloadButton;
}
private void LoadSettings()
{
if (!IsPostBack) { ShowFileList(); }
}
protected void Reload_Click(object sender, System.EventArgs e)
{
ShowFileList();
}
protected void date_TextChanged(object sender, EventArgs e)
{
try
{
Session["Date1"] = Date1.Text;
Session["Date2"] = Date2.Text;
ShowFileList();
}
}
catch (Exception ex)
{
ErrorLbl.Text = ErrorLbl.Text + "date_TextChanged: " + ex.Message;
return;
}
}
private void ShowFileList()
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("he-IL");
try
{
if (!Directory.Exists(@"files\\" + Context.User.Identity.Name))
Directory.CreateDirectory(@"files\\" + Context.User.Identity.Name);
}
catch (Exception ex)
{
ErrorLbl.Text = ex.Message ;
}
try
{
string dateString = null;
string file = null;
string User = Context.User.Identity.Name;
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("files\\" + Context.User.Identity.Name + "\\"));
System.IO.FileInfo[] diar1 = di.GetFiles();
System.IO.FileInfo dra = null;
// chklst.Items.Clear();
//loop through files, compare names to dates, show files with names in range.
foreach (System.IO.FileInfo dra_loopVariable in diar1)
{
dra = dra_loopVariable;
file = dra.ToString();
dateString = file.Remove(file.Length - 4, 4);
dateString = dateString.Substring(dateString.Length - 10);
dateString = dateString.Replace("_", "/");
DateTime convertedDate = System.DateTime.ParseExact(dateString, "dd/MM/yyyy", System.Threading.Thread.CurrentThread.CurrentCulture);
DateTime CDate1 = System.DateTime.ParseExact((string)Session["Date1"], "dd/MM/yyyy", System.Threading.Thread.CurrentThread.CurrentCulture);
DateTime CDate2 = System.DateTime.ParseExact((string)Session["Date2"], "dd/MM/yyyy", System.Threading.Thread.CurrentThread.CurrentCulture);
if (CDate1 <= convertedDate && convertedDate <= CDate2)
{
chklst.Items.Add(file);
}
}
}
catch (Exception ex)
{
ErrorLbl.Text = ex.Message;
}
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
}
public void FillDates()
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("he-IL");
try
{
if (Date2.Text == string.Empty)
{
Session["Date2"] = DateTime.Now.ToString("dd/MM/yyyy");
Session["Date1"] = DateTime.Now.AddDays(-10).ToString();
Date2.Text = DateTime.Now.ToString("dd/MM/yyyy");
}
if (Date1.Text == string.Empty)
{
string TodaysDate = DateTime.Now.ToString("dd/MM/yyyy");
Session["Date1"] = DateTime.Now.AddDays(-10).ToString("dd/MM/yyyy");
Date1.Text = DateTime.Now.AddDays(-10).ToString("dd/MM/yyyy");
}
}
catch (Exception ex)
{
ErrorLbl.Text = ErrorLbl.Text + "FillDates" + ex.Message;
}
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
return;
}
void DownloadButton_Click(object sender, EventArgs e)
{
Download1_Click1();
}
private void Download1_Click1()
{
try
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("he-IL");
bool emptyCheckboxList = false;
Int32 ItemsNum = chklst.Items.Count;
FileStream MyFileStream = default(FileStream);
long FileSize = 0;
StreamWriter fp = default(StreamWriter);
log4net.ILog log = default(log4net.ILog);
string exeFileName = Context.User.Identity.Name + "_" + DateTime.Now.ToString("ddMMyyyy") + "_" + DateTime.Now.ToString("HHmmss") + ".exe";
string zipFileName = Context.User.Identity.Name + "_" + DateTime.Now.ToString("ddMMyyyy") + "_" + DateTime.Now.ToString("HHmmss") + ".zip";
Response.Clear();
using (ZipFile zip = new ZipFile())
{
foreach (ListItem lstItem in chklst.Items)
{
ErrorLbl.Text = ErrorLbl.Text + "in checklist: " + lstItem.Text + "
";
if (lstItem.Selected == true)
{
emptyCheckboxList = true;
ErrorLbl.Text = ErrorLbl.Text + "downloaded: " + lstItem.Text + "
";
zip.AddFile(Server.MapPath("files\\" + Context.User.Identity.Name + "\\") + lstItem.Text, Context.User.Identity.Name + "_" + DateTime.Now.ToString("ddMMyyyy") + "_" + DateTime.Now.ToString("HHmmss"));
}
}
zip.Save(Server.MapPath("files\\temp\\") + zipFileName);
}
MyFileStream = new FileStream(Server.MapPath("files\\temp\\") + zipFileName, FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[Convert.ToInt32(FileSize) + 1];
MyFileStream.Read(Buffer, 0, Convert.ToInt32(FileSize));
MyFileStream.Close();
using (FileStream fs = new FileStream(Server.MapPath("files\\temp\\") + zipFileName, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] Buffer2 = new byte[2049];
Int32 n = default(Int32);
do
{
n = fs.Read(Buffer2, 0, 2048);
Response.OutputStream.Write(Buffer2, 0, n);
} while (n > 0);
}
Response.Flush();
Response.Close();
if (emptyCheckboxList == false)
{
ErrorLbl.Text = ErrorLbl.Text + "No File was chosen for download, please select files ";
}
}
catch (Exception ex)
{
ErrorLbl.Text = ErrorLbl.Text + "Download_Click: " + ex.Message;
return;
}
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
}
}
}