ASP.NET Web Forms File Upload in a User Control (ASCX)

ASP.NET Web Forms · Published · AjaxUploader team

A reusable upload component should fit into the page lifecycle without hiding who validates, saves and owns the uploaded file. Start with a small ASCX contract, then keep record-specific decisions in the hosting page.

Put the picker in an ASCX control

An ASP.NET Web Forms user control is useful when several pages need the same upload interface. It is different from an ASP.NET Core Tag Helper or a browser Web Component. An ASCX runs on the server and participates in the hosting page lifecycle; its file input still depends on the browser submitting a multipart request.

<%@ Control Language="C#" %>
<asp:Label ID="PickerLabel" runat="server" AssociatedControlID="Picker" Text="Attachment" />
<asp:FileUpload ID="Picker" runat="server" />
<script runat="server">
    public bool HasSelectedFile { get { return Picker.HasFile; } }
    public System.Web.HttpPostedFile SelectedFile
    {
        get { return Picker.PostedFile; }
    }
</script>

Save this as Controls/AttachmentPicker.ascx. The properties expose the current request. They do not store file bytes in ViewState, Session or a hidden field. Treat a missing selection as a normal state, especially after a validation error or another postback.

Let the parent page own the form and submit action

<%@ Register Src="~/Controls/AttachmentPicker.ascx"
    TagPrefix="local" TagName="AttachmentPicker" %>
<form id="MainForm" runat="server" enctype="multipart/form-data">
    <local:AttachmentPicker ID="Attachment" runat="server" />
    <asp:Button ID="Save" runat="server" Text="Save"
        OnClick="Save_Click" />
</form>

Use the page's existing server form when adding this markup to an application. Do not nest another form inside the ASCX. In Save_Click, check page validation and Attachment.HasSelectedFile before reading SelectedFile. Authorize the operation for the current record, apply server-side file restrictions and use an application-generated storage key.

If the control sits inside an UpdatePanel, the built-in FileUpload needs a full postback trigger for the submitting button. An asynchronous panel refresh is not a replacement for uploading the selected bytes. AjaxUploader provides a separate upload workflow; retain its documented handler and initialization rather than applying the built-in control trigger rule to every component.

Respect naming containers and repeated instances

Two instances can use the same internal Picker ID because their naming containers differ. Client-side code must use the rendered ClientID or a scoped selector, not assume the browser ID is literally Picker. Keep public properties about intent: allowed categories, whether an attachment is required and a label for the user. Keep database permissions and storage credentials outside the ASCX.

  • Render two instances and confirm the selected files stay separate.
  • Submit with no file, an allowed file and a rejected file.
  • Repeat the test inside the actual master page and UpdatePanel configuration.
  • Confirm the same component works after a validation error without suggesting that the browser kept the previous selection.

Choose the upload workflow before adding progress UI

The built-in control is a good baseline for a single file submitted with a form. When users need a queue, progress, retries or uploads before final form submission, use a component designed for that workflow. Reusing the interface does not make an upload permanent: the hosting application still needs a clear acceptance step and an authorized download path.

Common questions

Can an ASCX remember the selected file across a postback?

Do not rely on that. A browser file selection is not ordinary ViewState. Use an explicit temporary-upload workflow if a file must survive later form interactions.

Do I need a separate upload handler for every user control?

Usually no. Shared upload infrastructure can serve multiple instances, provided each request carries validated context and the server checks who may attach a file to the target record.

Official references

Examples describe the stated framework and application workflow. Check your installed component version and hosting configuration before adapting them.

Evaluate AjaxUploader for ASP.NET Web Forms