2

I have a problem regarding a ASP.NET fileuploader that is empty after postback. I have one fileupload control, one textbox control and one button control.

When I click the button, it checks if the textbox is populated, if it is it performs actions, if its not then it shows an error message and stay on the page. However, when I press the button and the textbox is empty, the button postsback and the fileuploader is empty and the user has to select the file again. So: User selects file > The file is located in the fileuploader > the user presses the button > the error message pops up > the fileuploader loses the file after postback.

When the user selected a file but not yet clicked the button (koala.jpg) enter image description here

When the button is pressed, the file is gone (no file selected)
enter image description here

How do I keep a file in the fileuploader after a postback or maybe tell the button not to post back if the textbox is empty?

EDIT:

Be aware that I simplified the question and that the content of the first post are just made up to make my question more clear.

Page load:

    protected void Page_Load(object sender, EventArgs e)
    {
        //First check if User is logged in 
        if (Session["User"] == null)
        {
            Response.Redirect(GetRouteUrl("Login", null));
        }

        TicketsBLL objTicketsBll = new TicketsBLL();


        var cat = objTicketsBll.getAllCategories();

        ddlCategory.DataTextField = "name";
        ddlCategory.DataValueField = "id";
        ddlCategory.DataSource = cat;
        ddlCategory.DataBind();



    }

Button Click:

//check if everthing is filled :
        errorContent.Visible = false;
        errorSubject.Visible = false;

        if (txtSubject.Text == "" || txtSubject.Text == null || txtContent.Text == null || txtContent.Text == "")
        {
            if (txtSubject.Text == "" || txtSubject.Text == null)
            {
                errorSubject.Visible = true;
            }
            if(txtContent.Text == null || txtContent.Text == ""){
                errorContent.Visible = true;
            }
        }

        else { //Do something after this

errorSubject & errorContent are hidden divs that show up as the error

Niek Jonkman
  • 477
  • 4
  • 11
  • 23
  • Can you post you code of Page_Load event? – User2012384 Dec 05 '13 at 07:56
  • Some comments about Button Click event, suggest that you use javascript to check whether the txtSubect.Text is empty or not, if it's empty, then show error message, also, I don't think txtSubject.Text will be null.. – User2012384 Dec 05 '13 at 08:05

2 Answers2

12

It's not possible to set the value of a file input. This is a browser security feature, it's the same with a password field, the only difference is with a password field you can set the value using JavaScript.

However with the file input you cannot set the value.

The solution is to prevent the postback, use JavaScript to check the value before posting back.

Phill
  • 18,398
  • 7
  • 62
  • 102
  • If I can not set the value, what should I do with the value when I use the JavaScript? If I keep the value I know that my program keeps the file but the user thinks the file is gone because the fileuploader says so. – Niek Jonkman Dec 05 '13 at 08:09
  • @NiekJonkman - use JavaScript to validate the textbox field has a value. If it has no value, don't submit the form. If you do allow the form to be posted back then the only solution is that the user has to select the file again. – Phill Dec 05 '13 at 09:14
1

As Phill pointed out you cannot update this Control at all from the code behind or from repopulating from the Form Values.

If you are relying on an auto postback to change data somewhere else on the form, you will need to handle the initial FileUpload and create a fake placeholder that indicates the file name that was/is to be uploaded.

The easier solution might be to wrap the other controls that require the AutoPostBack in a UpdatePanel so that they can be post back independent of the File Upload control.

Steve
  • 1,995
  • 2
  • 16
  • 25