I have an FileUpload control, where a user can upload pictures.
After the file is uploaded, i need to store in a variable like int currentFiles the number of files uploaded by the user, so if currentFiles >= 5 for example to disable the fileUpload control.
How can i store this variable and it's data even after postBack? (and could not be accesed/modified from browser)
But when expires this session? after the webadress is changed?
</div>
]
Session expires by default 20 minutes also you can destroy session varaiable by assigning null to it also by calling Session.Abandon() you can make it expire
If you only need to store the number of files uploaded and scope of the action is pagewise only, you have better use ViewState. But if you need to imply this throughout the life of application, you can use session, as you already have determined.
Get the number from ViewState (before uploading): int numberCount=(int)ViewState["UploadedFileCount"];
Store the number in ViewState (after uploading): ViewState["UploadedFileCount"]=numberCount+1;
If count is already 5, disable the upload control.
Hope this helps. Please feel free to ask if any problem persists or if any help is required. Thanks.
You'll have to either save this information in a file / database table for each user in case you want the user to be able to upload 5 pictures at most anytime.
That's because:
Application variables are destroyed when the web application is restarted.
Session variables are destroyed when the session ends / expires.
Viewstate variables are destroyed when the user browses away from current page.
Hope that helped.
I don't suffer from madness,
I enjoy it every minute of my life
Marked as answer by zuperboy90 on Mar 13, 2009 10:20 PM
But if i'm storring the FilesCount in a asp:hiddenField with Visible property = false (so it can't be viewed/modified in source) wouldn't that be a good choise?
Ok, the idea of using a hidden field is because is HIDDEN. So all you have to do is to use hidden fields with property runat="server" and they'll be accessible anytime.
Btw, you can actually see in the page code whatever is stored in a hidden field so if you want to keep the data hidden from user, using hidden fields is not advisable.
Another possibilities would be:
1) Store the value in a label / textbox with visible = false. Their value will be saved only in viewstate and they won't be rendered in page. But this is rather odd and you can directly use step 2.
2) Store the value in Viewstate
3) Store the value in Page.Cache
4) Store the value in Session
5) Store the value in Application (you'll have to create a special key for each user)
6) Store the value in the database (you'll have to use probably user id and counter column)
Options 1 - 3 deal with storing the value for current page access, that means if the user exits this page and comes back he will be able to upload more files.
Option 4 deals with user being able to upload 5 files only in this session. He will be forced to log out and log in again to be able to upload more files.
Option 5 blocks the user from uploading more than 5 files until the application is restarted on server due to update / IIS restart / server restart etc.
Option 6 effectively blocks the user from uploading more than 5 files until you actually change the rule.
You'll choose whatever suits your needs from these options.
I don't suffer from madness,
I enjoy it every minute of my life
instead of creating a new connection to database, can't i use Profile property and add Profile.Uploads = 0 when pageLoads, and after a file is uploaded then Profile.Uploads++ and i verifiy if profile.uploads >= 5 when a file is uploaded, for example?
zuperboy90
Participant
977 Points
819 Posts
storing a variable value after postBack
Mar 12, 2009 12:35 PM|LINK
Hello
I have an FileUpload control, where a user can upload pictures.
After the file is uploaded, i need to store in a variable like int currentFiles the number of files uploaded by the user, so if currentFiles >= 5 for example to disable the fileUpload control.
How can i store this variable and it's data even after postBack? (and could not be accesed/modified from browser)
Thank you
mohd786hussa...
Contributor
4329 Points
878 Posts
Re: storing a variable value after postBack
Mar 12, 2009 01:09 PM|LINK
You can store the value in session variable and check it on page load.
Mohammad Hussain
Web design, Logo design & Asp.net development
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: storing a variable value after postBack
Mar 12, 2009 01:14 PM|LINK
you can save the HTTPPosted file in session
//To save ArrayList arr = new ArrayList();arr.Add(FileUpload1.PostedFile);
Session["UploadedFiles"] = arr;
//To get back ArrayList arr = (ArrayList)Session["UploadedFiles"]; //To get back the first file HttpPostedFile postedfile = (HttpPostedFile)arr[0];Contact me
zuperboy90
Participant
977 Points
819 Posts
Re: storing a variable value after postBack
Mar 12, 2009 07:50 PM|LINK
ok...
I will use something like Session["upFiles"] = 0;
But when expires this session? after the webadress is changed?
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: storing a variable value after postBack
Mar 13, 2009 04:39 AM|LINK
Session expires by default 20 minutes also you can destroy session varaiable by assigning null to it also by calling Session.Abandon() you can make it expire
Contact me
sangam100
Participant
1639 Points
316 Posts
Re: storing a variable value after postBack
Mar 13, 2009 06:53 AM|LINK
Hi zuperboy90 ,
If you only need to store the number of files uploaded and scope of the action is pagewise only, you have better use ViewState. But if you need to imply this throughout the life of application, you can use session, as you already have determined.
Get the number from ViewState (before uploading):
int numberCount=(int)ViewState["UploadedFileCount"];
Store the number in ViewState (after uploading):
ViewState["UploadedFileCount"]=numberCount+1;
If count is already 5, disable the upload control.
Hope this helps. Please feel free to ask if any problem persists or if any help is required. Thanks.
asp.net ViewState Session
Very useful visual studio shortcuts and more tips and tricks
5 Different ways to open new window in asp.net
Dyno1979b
Participant
1764 Points
347 Posts
Re: storing a variable value after postBack
Mar 13, 2009 12:21 PM|LINK
You'll have to either save this information in a file / database table for each user in case you want the user to be able to upload 5 pictures at most anytime.
That's because:
Application variables are destroyed when the web application is restarted.
Session variables are destroyed when the session ends / expires.
Viewstate variables are destroyed when the user browses away from current page.
Hope that helped.
I enjoy it every minute of my life
zuperboy90
Participant
977 Points
819 Posts
Re: storing a variable value after postBack
Mar 13, 2009 10:23 PM|LINK
But if i'm storring the FilesCount in a asp:hiddenField with Visible property = false (so it can't be viewed/modified in source) wouldn't that be a good choise?
PS Dyno, we are neighbors [:P] salutari
Dyno1979b
Participant
1764 Points
347 Posts
Re: storing a variable value after postBack
Mar 13, 2009 10:36 PM|LINK
Kinda ... 400+ km [:D]
Ok, the idea of using a hidden field is because is HIDDEN. So all you have to do is to use hidden fields with property runat="server" and they'll be accessible anytime.
Btw, you can actually see in the page code whatever is stored in a hidden field so if you want to keep the data hidden from user, using hidden fields is not advisable.
Another possibilities would be:
1) Store the value in a label / textbox with visible = false. Their value will be saved only in viewstate and they won't be rendered in page. But this is rather odd and you can directly use step 2.
2) Store the value in Viewstate
3) Store the value in Page.Cache
4) Store the value in Session
5) Store the value in Application (you'll have to create a special key for each user)
6) Store the value in the database (you'll have to use probably user id and counter column)
Options 1 - 3 deal with storing the value for current page access, that means if the user exits this page and comes back he will be able to upload more files.
Option 4 deals with user being able to upload 5 files only in this session. He will be forced to log out and log in again to be able to upload more files.
Option 5 blocks the user from uploading more than 5 files until the application is restarted on server due to update / IIS restart / server restart etc.
Option 6 effectively blocks the user from uploading more than 5 files until you actually change the rule.
You'll choose whatever suits your needs from these options.
I enjoy it every minute of my life
zuperboy90
Participant
977 Points
819 Posts
Re: storing a variable value after postBack
Mar 14, 2009 09:18 AM|LINK
instead of creating a new connection to database, can't i use Profile property and add Profile.Uploads = 0 when pageLoads, and after a file is uploaded then Profile.Uploads++ and i verifiy if profile.uploads >= 5 when a file is uploaded, for example?