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" %>
<portal:mojoPanel ID="mp1" runat="server" ArtisteerCssClass="art-Post" RenderArtisteerBlockContentDivs="true" >
<mp:CornerRounderTop id="ctop1" runat="server" />
<asp:Panel ID="pnlWrapper" runat="server" cssclass="art-Post-inner panelwrapper DownloadFormModule">
<portal:ModuleTitleControl EditText="Edit" runat="server" id="TitleControl" />
<portal:mojoPanel ID="MojoPanel1" runat="server" ArtisteerCssClass="art-PostContent">
<asp:Panel ID="pnlDownloadFormModule" runat="server" CssClass="modulecontent">
<fieldset>
<center>
<table >
<tr>
<td valign="top">
<div id="dates" runat="server" visible="True">
<fieldset>
<legend>Dates</legend>
<div>
<div class="area">
<div class="heading">Choose different Dates</div>
<br />
<b>Start Date: </b><br />
<asp:TextBox runat="server" ID="Date1" text="" AutoPostBack="true" />
<ajaxToolkit:CalendarExtender ID="defaultCalendarExtender" runat="server" TargetControlID="Date1" Format="dd/MM/yyyy" />
<asp:Image runat="Server" ID="Image2" ImageUrl="~/App_DesignTimeStyle/Calendar_scheduleHS.png" />
<div style="font-size: 90%"><em>(Set the focus to the textbox to show the calendar)</em></div>
<b>End Date: </b><br />
<asp:TextBox runat="server" ID="Date2" text="" AutoPostBack="true" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="Date2" Format="dd/MM/yyyy" />
<asp:Image runat="Server" ID="Image1" ImageUrl="~/App_DesignTimeStyle/Calendar_scheduleHS.png" />
</div>
</div>
</fieldset>
</div>
</td>
<td valign="top" >
<div id="Files" runat="server" visible="True">
<fieldset>
<legend>Files</legend>
<div class="demoheading">Download Files</div>
<asp:CheckBoxList id="chklst" runat="server" /><br />
<script type="text/javascript">
function fnCheckAll() {
var i = 0;
var item;
while (true) {
item = document.getElementById("ctl00_mainContent_chklst_" + i);
if (item == null)
break;
item.checked = true;
i++;
}
}
function fnClearAll() {
var i = 0;
var item;
while (true) {
item = document.getElementById("ctl00_mainContent_chklst_" + i);
if (item == null)
break;
item.checked = false;
i++;
}
}
</script>
<asp:button id="chkButton" runat="server" Height="23px" Width="72px" Text="Select All" ></asp:button>
<asp:button id="clrButton" runat="server" Height="23px" Width="72px" Text="Clear" ></asp:button>
<asp:button id="Reload" runat="server" Height="23px" Width="72px" Text="List Files" OnClick="Reload_Click" AutoPostBack="true"></asp:button>
<asp:Button ID="DownloadButton" runat="server" AutoPostBack="true" />
</fieldset>
</div>
</td>
</tr>
</table>
<P>
<asp:label id="ErrorLbl" runat="server" Height="30px" Width="695px" ForeColor="Red"></asp:label>
</P>
</center>
</fieldset>
</asp:Panel>
</portal:mojoPanel>
<div class="cleared"></div>
</asp:Panel>
<mp:CornerRounderBottom id="cbottom1" runat="server" />
</portal:mojoPanel>
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 + "<br />";
if (lstItem.Selected == true)
{
emptyCheckboxList = true;
ErrorLbl.Text = ErrorLbl.Text + "downloaded: " + lstItem.Text + "<br />";
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");
}
}
}