1

I have an aspx form that contains a series of user inputs and then a submit button that packages that info and sends an email with the parsed data and any attachments.

I'm using an asp:fileupload for the user to attach files to the email, and on the button click it hits a method i wrote in the codebehind that checks to ensure all the required fields were populated, if it's not I kick back an error message popup and focus the first required field that fails. The problem is after the pushback it loses the attachment. So I need to be able to check my fields client side and my background is in C++ so scripting is new to me. Any help is appreciated.

part of my form for visual reference: https://i.stack.imgur.com/Ioawg.png

showing how I'm doing error handling currently https://i.stack.imgur.com/PhfQw.png

3 Answers3

1

On postback, the uploadfile will clear; this cannot be prevented. But there are workarounds.

  1. First, if possible, add RequiredValidators to check for missing data on the client side prior to submitting the form.

    <asp:textbox id="Text1" text="Enter a value" runat="server" />
        <asp:requiredfieldvalidator id="RequiredFieldValidator1" controltovalidate="Text1"
            text="Required Field!" runat="server" />
        <asp:button id="Button1" runat="server" text="Validate" />
        <asp:validationsummary id="valSum" displaymode="BulletList" runat="server" headertext="You must enter a value in the following fields:" />
    

You could also write some javascript to do the auto-focus you are looking for.

  1. If you really need to submit the form and validate it on the codebehind, save the posted file to a temporary folder on the server with a random file name. Update a hidden label or ViewState on the form with the file name so you can get the file back from the server on the next postback. You can hide the upload control and replace it with a label "File {name} ready for upload" and a button "upload different file".

Note: Do NOT put the actual file contents in viewState or session. That is not going to be good for performance.

Amos Zoellner
  • 408
  • 4
  • 10
  • I'm familiar with asp's built in validator but it seems sloppy to me. I don't want it popping error text up on the form, my errors are done by jquery to pop up in their own windows. Ideally I'd like some kind of script that runs client side to check those fields before I go to code behind. I feel like it should be really simple but I'm missing some piece of the puzzle. – Dylan Schmidt Mar 18 '15 at 13:09
  • You can write a js function that returns false if your fields are filled in, else true, and set the button onclientclick to 'return myTestFunction()'. – Amos Zoellner Mar 19 '15 at 16:20
  • That's what I'd like to do, but my js knowledge is lacking. How do I reference the form controls from the script? – Dylan Schmidt Mar 23 '15 at 13:01
1

When you first retrieve the contents of the file, persist them somewhere such as ViewState, Session, or database. Then you only need to receive the file once.

To avoid confusion, you can make retrieving the file and gathering the rest of the form inputs into separate steps in your workflow.

mason
  • 31,774
  • 10
  • 77
  • 121
0

This is a security feature with the post back mechanism. The server can't retain any information regarding where the physical file gets stored. It only knows that you sent a series of bytes into a field.

One way around this is to use AJAX to persist the state in the browser. You may look at UpdatePanel or do a simple implementation yourself.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29