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?

Recommended Answers

All 4 Replies

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:

function detectFile()
{
  var idval = document.getElementById("servercontrol").value;

  if (idval.length > 5)
  {
    if (idval.indexOf(":") && idval.indexOf(".")
    {
      document.getElementById("serversubmitbtn").click();
    }
  }
}

'put attributes on it:
ctrl.Attributes.Add("OnChange","retun detectFile()")

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.

You are awsome.

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:

function detectFile()
{
  var idval = document.getElementById("servercontrol").value;

  if (idval.length > 5)
  {
    if (idval.indexOf(":") && idval.indexOf(".")
    {
      document.getElementById("serversubmitbtn").click();
    }
  }
}

'put attributes on it:
ctrl.Attributes.Add("OnChange","retun detectFile()")

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!!!

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:

document.getElementById("<%= ServerControl.ClientID %>");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.