0

I'm developing a page that will have an upload control on it. The control is quite complex and doesn't postback whilst uploading files. It works perfectly fine when not in a form tag with runat="server" as it has it's own form with a post action that will post to a ashx file.

The problem is that I now want to put this control within a page amongst other asp.net controls that work via runat="server". This means that all the controls around it will need to be within a form tag with runat="server".

Here is the form that the control works within :

<form action="MyFileTransfer.ashx" method="post" enctype="multipart/form-data">

   // buttons etc.

</form>

Althought if I put in in my master page with <form runat="server"></form> wrapping it's entire content, I get issues.

What is the best way to approach this and what do you recommend?

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123

4 Answers4

1

I am not sure if I understand what exactly triggers the upload, but let's say some button is doing the form submit. Normally this is to the page where all of your controls are on, but you want it to post to MyFileTransfer.ashx.

So one solution would be the following steps, which you will have to perform on the clientside with JavaScript.

  1. Dynamically create a invisible IFRAME

  2. Set the target of the form to the ID of the IFRAME

  3. Store the old form action

  4. Set the action of the form to MyFileTransfer.ashx

  5. Submit form

  6. Remove target from form

  7. Restore the old action of the form.

The above steps could be to simple for your scenario but works in the general case. Hope this puts you in the right direction of solving your problem.

BTW. I presume you're already using nested FORMS which isn't allowed. A FORM element can't be nested, some browser will let you do it but that doesn't mean you should.

Martijn B
  • 4,065
  • 2
  • 29
  • 41
  • I would like to report that your answer helped me alot. I was trying to get an jquery file upload (blueimp's) to work on my asp.net site. On it's own it worked, once i tried to put it in my site (and surround it with form runat="server") it started giving silent errors. Putting the entire contraption in an iframe solved the issue. – Иво Недев Nov 13 '15 at 15:44
0

I think you can put 2 forms on one page. There is no error as long as only one has runat="server"

Following article describe more details: Form-based Programming in ASP.NET.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
findcaiyzh
  • 647
  • 3
  • 7
  • I know I can put multiple forms on a page. I thought I had highlighted that in my question. It's the functionality changing when putting my POST form inside the runat="server" form. – Lloyd Powell Feb 15 '12 at 12:13
  • Do you mean you want to post file and other field together back? if so you have to handle the file in code behind instead of your MyFileTransfer.ashx – findcaiyzh Feb 15 '12 at 12:42
0

I do not that you have luck with this one because ether the redirect can not work in your case, ether you cant change the parameters of the form on fly because the post back is all ready done for the upload. Also you can not use the same enctype for all post data because here you try to upload files.

One Possible solution is to add your upload control inside an iframe. And because the control can change after the update, you make a script that update the height and width of the iframe after the post back automatically.

Relative to change the iframe height:

Scrolling iframe with main page side bar

make iframe height dynamic based on content inside- JQUERY/Javascript

By the way, the iframe is not bad idea, other similar controls use flash load inside iframe to make the upload. You can also do it with inside dialog for open this iframe like the iframe content in this examples http://highslide.com/#examples

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
-2

Better to use ajax update panel around the upload

Prabhavith
  • 458
  • 1
  • 4
  • 15