0

I have a little problem with a page im trying to create. In resume, I want to validate if a file has been selected by the user and then upload it on the server. I know it is possible to do it throught javascript :

if(document.getElementById("uploadBox").value != "") {
   // you have a file
}

But the thing is if I add the property runat="server" to my input :

<input id="myFile" type="file" runat="server" />

I can't validate anymore. So, do you know a way to do what I am trying to do.

PS: Keep in mind that I want to validate with javascript to catch the postback and then show an error message using ajax. That's why im not doing the validation trough server code.

Thank you :)

1 Answer 1

1

That happens because the ID of the input control is changed when you run it server side. Here are two ways to solve it, either force the ID to stay the same with this:

<input id="myFile" type="file" runat="server" ClientIDMode="Static" />

or, if you have the Javascript in the same ASP page, you can write the ID directly like this:

if(document.getElementById('<%= uploadBox.ClientID %>').value != "") {
   // you have a file
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.