The stutiation illustrate as the followsing statement.
I use two pages, one, named "upload.aspx", is including several general html input tag, whose type are "file". That mean I create server file upload controls in this page.
After user choose their files, wanted to upload, and click upload button, this page will be postbacked to another page, called "edit.aspx".
The code as follows.
edit.aspx
Expand|Select|Wrap|Line Numbers
- (.cs)
- protected void Page_Load(Object send, EventArgs e){
- if (PreviousPage != null)
- {
- if (PreviousPage.IsCrossPagePostBack == true)
- this.SaveImages();
- }
- if(IsPostBack){
- this.SaveImage();
- }
- }
As many knows, the web control, dynamic generated, must be created again for keep their state while posback occurs. My web controls in edit aspx, including TextBox, DropDownList and Label control, depend on how many file to be choosed to upload from Upload.aspx.
edit.aspx
Expand|Select|Wrap|Line Numbers
- .cs
- private System.Boolean SaveImages()
- {
- Control form = FindControl("form1");
- //loop through the files uploaded
- System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
- //Message to the user
- System.Text.StringBuilder _message = new System.Text.StringBuilder("Files Uploaded:<br>");
- try
- {
- for (System.Int32 _iFile = 0; _iFile < _files.Count; _iFile++)
- {
- // Check to make sure the uploaded file is a jpg or gif
- System.Web.HttpPostedFile _postedFile = _files[_iFile];
- System.String _fileName, _fileExtension;
- _fileName = System.IO.Path.GetFileName(_postedFile.FileName);
- _fileExtension = System.IO.Path.GetExtension(_fileName);
- if ((_fileExtension == ".jpg") || (_fileExtension == ".JPG") || (_fileExtension == ".jpeg") || (_fileExtension == ".JPEG"))
- {
- //Save File to the proper directory
- _postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("jpgs/") + _fileName);
- labels[_iFile,0].Text = "Album Name :";
- labels[_iFile, 1].Text = "Title :";
- labels[_iFile, 2].Text = "Tag :";
- labels[_iFile, 3].Text = "Description :";
- textboxs[_iFile, 0].Text ="";
- textboxs[_iFile, 1].Text = _fileName;
- textboxs[_iFile, 2].Text ="";
- textboxs[_iFile, 3].Text ="";
- images[_iFile].ImageUrl = "./jpgs/" + _fileName;
- panels[_iFile].Controls.Add(labels[_iFile, 0]);
- panels[_iFile].Controls.Add(droplists[_iFile, 0]);
- panels[_iFile].Controls.Add(new LiteralControl("<a href='#' onclick='popup();'>add</a>"));
- panels[_iFile].Controls.Add(new LiteralControl("<br>"));
- panels[_iFile].Controls.Add(labels[_iFile, 1]);
- panels[_iFile].Controls.Add(droplists[_iFile, 1]);
- panels[_iFile].Controls.Add(new LiteralControl("<a href='#' onclick='popup();'>add</a>"));
- panels[_iFile].Controls.Add(new LiteralControl("<br>"));
- panels[_iFile].Controls.Add(labels[_iFile, 2]);
- panels[_iFile].Controls.Add(textboxs[_iFile, 2]);
- panels[_iFile].Controls.Add(new LiteralControl("<br>"));
- panels[_iFile].Controls.Add(labels[_iFile, 3]);
- panels[_iFile].Controls.Add(textboxs[_iFile, 3]);
- panels[_iFile].Controls.Add(new LiteralControl("<br>"));
- panels[_iFile].Controls.Add(images[_iFile]);
- form.Controls.Add(panels[_iFile]);
- form.Controls.Add(conform);
- form.Controls.Add(cancel);
- _message.Append(_fileName + "<BR>");
- }
- else
- {
- if (_fileExtension != "")
- {
- _message.Append(_fileName + " <font color=\"red\">failed!! Only .gif and .jpg images allowed!</font> <BR>");
- }
- }
- }
- Label5.Text = _message.ToString();
- return true;
- }
- catch (System.Exception Ex)
- {
- // Label1.Text = Ex.Message;
- return false;
- }
As I know, these web controls must be created again, but my code will check System.Web.HttpPostedFile _postedFile object to decide whether web controls need to be created.
So during postback, no file upload to be choosed cause my edit.aspx page to be empty.
Could any one to help me how to keep my page.
Using ViewState? session?
Thanks for anyone kindly help.
Jim.