943,948 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 12510
  • ASP.NET RSS
Mar 4th, 2008
0

Force Postback on FileUpload control

Expand Post »
Hi - I was wondering if there is a way to force a postback after the FileUpload1 control has a file... Right now, people his "Browse" select the file then click another button to upload the file. I want to write code on postback to automatically write the file to the server, so I would like to cause a postback event when it has a file... any idea how to do this?
Team Colleague
Reputation Points: 14
Solved Threads: 29
Posting Whiz in Training
ericstenson is offline Offline
293 posts
since Dec 2007
Mar 4th, 2008
0

Re: Force Postback on FileUpload control

You cannot detect when it has a file, however you can detect length. If you wish to detect length, you need to make sure the box is READONLY. Cause, like me and probably a few others, I type most of my paths into the upload box. This you would have to do client side, so javascript would be best bet. Something like this:
ASP.NET Syntax (Toggle Plain Text)
  1. function detectFile()
  2. {
  3. var idval = document.getElementById("servercontrol").value;
  4.  
  5. if (idval.length > 5)
  6. {
  7. if (idval.indexOf(":") && idval.indexOf(".")
  8. {
  9. document.getElementById("serversubmitbtn").click();
  10. }
  11. }
  12. }
  13.  
  14. 'put attributes on it:
  15. ctrl.Attributes.Add("OnChange","retun detectFile()")
  16.  
Now you need a button with this one, and you should always have it just incase javascript is disabled. What this does is checks to see if the length of your file upload control is greater than 5 (C:/ & .tag is easily over 5 combined, let alone the actual file name), if it is, it then checks to make sure that there is at least a colon and a period. You can change this extensively and use regex if you wish. Then it calls your submit button's onclick attribute dynamically.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Mar 4th, 2008
0

Re: Force Postback on FileUpload control

You are awsome.
Team Colleague
Reputation Points: 14
Solved Threads: 29
Posting Whiz in Training
ericstenson is offline Offline
293 posts
since Dec 2007
Jan 20th, 2009
0

Re: Force Postback on FileUpload control

You cannot detect when it has a file, however you can detect length. If you wish to detect length, you need to make sure the box is READONLY. Cause, like me and probably a few others, I type most of my paths into the upload box. This you would have to do client side, so javascript would be best bet. Something like this:
ASP.NET Syntax (Toggle Plain Text)
  1. function detectFile()
  2. {
  3. var idval = document.getElementById("servercontrol").value;
  4.  
  5. if (idval.length > 5)
  6. {
  7. if (idval.indexOf(":") && idval.indexOf(".")
  8. {
  9. document.getElementById("serversubmitbtn").click();
  10. }
  11. }
  12. }
  13.  
  14. 'put attributes on it:
  15. ctrl.Attributes.Add("OnChange","retun detectFile()")
  16.  
Now you need a button with this one, and you should always have it just incase javascript is disabled. What this does is checks to see if the length of your file upload control is greater than 5 (C:/ & .tag is easily over 5 combined, let alone the actual file name), if it is, it then checks to make sure that there is at least a colon and a period. You can change this extensively and use regex if you wish. Then it calls your submit button's onclick attribute dynamically.
Can help me a bit for using this Java code. It looks very useful! We use VB.NET and how do I integrate your code with our FileUpload1 control on our page. Do we need to integrate your script with a button also? Please explain. Thank you!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hapdx is offline Offline
1 posts
since Jan 2009
Jan 21st, 2009
0

Re: Force Postback on FileUpload control

That code is javascript, so it is independent of any language run on the server. Now where it says "serversubmitbtn" or "servercontrol", you will want to put the ID of the server control right there. A method for this follows:

VB Syntax (Toggle Plain Text)
  1. document.getElementById("<%= ServerControl.ClientID %>");
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC