473,498 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to keep web controls' state from previous page and during postback in asp.net

JimWu
14 New Member
I have some problems in my code.
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
  1.   (.cs)
  2.  
  3. protected void Page_Load(Object send, EventArgs e){
  4.  
  5.    if (PreviousPage != null)
  6.         {
  7.             if (PreviousPage.IsCrossPagePostBack == true)
  8.  
  9.                 this.SaveImages();
  10.  
  11.       }
  12.   if(IsPostBack){
  13.            this.SaveImage();
  14.    }
  15.  
  16. }
  17.  
  18.  

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
  1. .cs
  2.  private System.Boolean SaveImages()
  3.     {
  4.         Control form = FindControl("form1");
  5.               //loop through the files uploaded
  6.         System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
  7.         //Message to the user
  8.         System.Text.StringBuilder _message = new System.Text.StringBuilder("Files Uploaded:<br>");
  9.         try
  10.         {
  11.             for (System.Int32 _iFile = 0; _iFile < _files.Count; _iFile++)
  12.             {
  13.                 // Check to make sure the uploaded file is a jpg or gif
  14.                 System.Web.HttpPostedFile _postedFile = _files[_iFile];
  15.                 System.String _fileName, _fileExtension;
  16.                 _fileName = System.IO.Path.GetFileName(_postedFile.FileName);
  17.                 _fileExtension = System.IO.Path.GetExtension(_fileName);
  18.              if ((_fileExtension == ".jpg") || (_fileExtension == ".JPG") || (_fileExtension == ".jpeg") || (_fileExtension == ".JPEG"))
  19.                 {
  20.  
  21.                     //Save File to the proper directory 
  22.                     _postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("jpgs/") + _fileName);
  23.                     labels[_iFile,0].Text = "Album Name :";
  24.  
  25.                     labels[_iFile, 1].Text = "Title :";
  26.                     labels[_iFile, 2].Text = "Tag :";
  27.                     labels[_iFile, 3].Text = "Description :";
  28.                     textboxs[_iFile, 0].Text ="";
  29.                     textboxs[_iFile, 1].Text = _fileName;
  30.                     textboxs[_iFile, 2].Text ="";
  31.                     textboxs[_iFile, 3].Text ="";
  32.                     images[_iFile].ImageUrl = "./jpgs/" + _fileName;
  33.                     panels[_iFile].Controls.Add(labels[_iFile, 0]);
  34.                     panels[_iFile].Controls.Add(droplists[_iFile, 0]);
  35.                     panels[_iFile].Controls.Add(new LiteralControl("<a href='#' onclick='popup();'>add</a>"));
  36.                     panels[_iFile].Controls.Add(new LiteralControl("<br>"));
  37.  
  38.                     panels[_iFile].Controls.Add(labels[_iFile, 1]);
  39.                     panels[_iFile].Controls.Add(droplists[_iFile, 1]);
  40.                     panels[_iFile].Controls.Add(new LiteralControl("<a href='#' onclick='popup();'>add</a>"));
  41.                     panels[_iFile].Controls.Add(new LiteralControl("<br>"));
  42.                     panels[_iFile].Controls.Add(labels[_iFile, 2]);
  43.                     panels[_iFile].Controls.Add(textboxs[_iFile, 2]);
  44.                     panels[_iFile].Controls.Add(new LiteralControl("<br>"));
  45.                     panels[_iFile].Controls.Add(labels[_iFile, 3]);
  46.                     panels[_iFile].Controls.Add(textboxs[_iFile, 3]);
  47.                     panels[_iFile].Controls.Add(new LiteralControl("<br>"));
  48.                     panels[_iFile].Controls.Add(images[_iFile]);
  49.                     form.Controls.Add(panels[_iFile]);
  50.                     form.Controls.Add(conform);
  51.                     form.Controls.Add(cancel);
  52.                     _message.Append(_fileName + "<BR>");
  53.                 }
  54.                 else
  55.                 {
  56.                     if (_fileExtension != "")
  57.                     {
  58.                         _message.Append(_fileName + " <font color=\"red\">failed!! Only .gif and .jpg images allowed!</font> <BR>");
  59.                     }
  60.                 }
  61.  
  62.             }
  63.             Label5.Text = _message.ToString();
  64.             return true;
  65.         }
  66.         catch (System.Exception Ex)
  67.         {
  68.            // Label1.Text = Ex.Message;
  69.             return false;
  70.         }
  71.  
  72.  
If I want to show above controls, the first time is ok. Now, I want to enable another button to click to postback on Edit.aspx, but edit.aspx is empty while doing postback.

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.
Aug 26 '07 #1
0 1822

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3488
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
8
4262
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
1
2660
by: Scott Schluer | last post by:
Hello, I've got myself a small problem and I'm hoping someone can help. I have a DataList called dlProducts (displays products from a database). Within the <ItemTemplate> container of the...
4
2488
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
4
6709
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle...
22
2143
by: Mr Newbie | last post by:
I was thinking about developing a workflow application yesterday and was musing over the different approaches than one could take in restricting specific actions on a ticket( Form ) at any said...
8
1975
by: mark.norgate | last post by:
I've asked this question before, but still haven't solved it, so am asking again. I am programmatically adding a user control to the page in response to a button click. The user control consists...
15
3590
by: mc | last post by:
I'm writing an app for managing Task Lists, I'm trying to add some controls to a form that I can use to link tasks, my original intention was to: - Add two list boxes, one listing "all Tasks"...
0
2220
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.