Postback does not work if a button is clicked!
<head runat="server">
<title>Untitled Page</title>
<script src="ckeditor/ckeditor.js" type="text/javascript" />
<style type="text/css">
.x-label-text
{
font-weight: bold;
font-size: 12px;
}
.div_detail
{
width: 99%;
background-color: #f2f2f2;
padding: 3px;
border: 1px solid #ccc;
}
.bluebutton
{
border-style: solid;
border-width: 1px;
}
</style>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div>
<textarea id="txt_Content3" name="txt_Content3" rows="10" cols="80"></textarea>
<table>
<tr>
<td colspan="3">
<input class="bluebutton" id="FindFile" style="width: 600px; height: 22px" type="file"
size="26" runat="server" name="FindFile">
</td>
</tr>
<tr>
<td colspan="3">
<asp:ListBox ID="ListBox1" runat="server" CssClass="txtbox" Height="100px" Width="600px"
Font-Size="XX-Small"></asp:ListBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="AddFile" runat="server" CssClass="bluebutton" Height="23px" Width="72px"
Text="Add"></asp:Button>
</td>
<td>
<asp:Button ID="RemvFile" runat="server" CssClass="bluebutton" Height="23px" Width="72px"
Text="Remove"></asp:Button>
</td>
<td>
<input class="bluebutton" id="Upload" style="width: 71px; height: 24px" type="submit"
value="Upload" runat="server" onserverclick="Upload_ServerClick" name="Upload">
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Label1" runat="server" Height="25px" Width="249px"></asp:Label>
</td>
</tr>
</table>
<script type="text/javascript">
CKEDITOR.replace( 'txt_Content3', { filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?type=Images', filebrowserFlashBrowseUrl
: 'ckfinder/ckfinder.html?type=Flash', filebrowserUploadUrl : 'ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files',
filebrowserImageUploadUrl : 'ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images',
filebrowserFlashUploadUrl : 'ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash',
filebrowserWindowWidth : '500', filebrowserWindowHeight : '500' } );
</script>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class MultiFileUploading : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlGenericControl txtOutput;
public ArrayList files = new ArrayList();
static public ArrayList hif = new ArrayList();
public int filesUploaded = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.RemvFile.Click += new System.EventHandler(this.RemvFile_Click);
this.AddFile.Click += new System.EventHandler(this.AddFile_Click);
this.Upload.ServerClick += new System.EventHandler(this.Upload_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// AddFile will add the path of the client side file that is currently in the
/// PostedFile
/// property of the HttpInputFile control to the listbox.
private void AddFile_Click(object sender, System.EventArgs e)
{
if (Page.IsPostBack == true)
{
hif.Add(FindFile);
ListBox1.Items.Add(FindFile.PostedFile.FileName);
}
else
{
}
}
/// RemvFile will remove the currently selected file from the listbox.
private void RemvFile_Click(object sender, System.EventArgs e)
{
if(ListBox1.Items.Count != 0)
{
hif.RemoveAt(ListBox1.SelectedIndex);
ListBox1.Items.Remove(ListBox1.SelectedItem.Text);
}
}
/// Upload_ServerClick is the server side script that will upload the files to
/// the web server
/// by looping through the files in the listbox.
public void Upload_ServerClick(object sender, System.EventArgs e)
{
string baseLocation = "C:\\temp\\";
string status = "";
if((ListBox1.Items.Count == 0) && (filesUploaded == 0))
{
Label1.Text = "Error - a file name must be specified.";
return;
}
else
{
foreach(System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
{
try
{
string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
HIF.PostedFile.SaveAs(baseLocation + fn);
filesUploaded++;
status += fn + "<br>";
}
catch(Exception err)
{
Label1.Text = "Error saving file " + baseLocation
+ "<br>" + err.ToString();
}
}
if(filesUploaded == hif.Count)
{
Label1.Text = "These " + filesUploaded + " file(s) were "
+ "uploaded:<br>" + status;
}
hif.Clear();
ListBox1.Items.Clear();
}
}
}
Thanks in advance