How to clear the textbox content in a FileUpload control in ASP.NET?
The ASP.NET FileUpload
control maps to the HTML input
element with type="file"
. This element is considered Read-only and you cannot change it directly.
However, there seem to be atleast three workarounds to accomplish the goal of "clearing the field" :
a. Reset the form, either using script or by providing a input type="reset"
button.
b. Re-establish the input field in the DOM by setting its attributes again:
var fu = document.getElementById("fileUpload");
if (fu != null)
{
fu.setAttribute("type", "input");
fu.setAttribute("type", "file");
}
c. Recreate the innerHTML
of the field from the existing innerHTML
as demonstrated here:
var fu = document.getElementById("fileUpload");
if (fu != null)
{
// You may be able to use the cached object in `fu`, but I'm not sure.
document.getElementById("fileUpload").innerHTML = fu.innerHTML;
}
Hi nice trick but for me following worked instead of innerhtml
var fu = document.getElementById("flUpload");
if (fu != null) {
document.getElementById("flUpload").outerHTML = fu.outerHTML;
}
regards, Harish
I have just Used the following in the Button click event in the code behind ...
protected void btnReset_Click(object sender, EventArgs e)
{
flUpload = new FileUpload();
}
The postback caused by the button click allows a page refresh and creates a new instance of the FileUpload control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With