Back to Devexpress

Page Life Cycle During File Upload

aspnet-16891-components-file-management-file-upload-concepts-page-life-cycle-during-file-upload.md

latest5.5 KB
Original Source

Page Life Cycle During File Upload

  • Jun 21, 2024
  • 3 minutes to read

When a file is being uploaded by ASPxUploadControl on the server side, the ASP.NET page life cycle is changed. After a file is uploaded and file upload events (FileUploadComplete and FilesUploadComplete) are raised, the request is aborted and only the Unload event is raised (see the diagram below).

Since file upload can be performed on two different control events (based on the FileUploadMode property value), there are two alternative page life cycles during file upload (illustrated in gold and green in the diagram below).

In the image above, the blue line illustrates a standard page life cycle. The gold and green lines illustrate the page life cycles during file upload.

  • When the FileUploadMode property is set to BeforePageLoad (the green line), file upload is performed on the control’s Init event, which is called before the Page_Init event. Note that at this time, other controls on the page are not completely initialized, and are not available in file upload events. After file upload is complete, the request is aborted and the Unload event is called.
  • When the FileUploadMode property is set to OnPageLoad (the gold line), the file upload is performed on the control’s Load event, which is called after the Page_Load event. After file upload is complete, the request is aborted and the Unload event is called.

Creating ASPxUploadControl at Runtime

When ASPxUploadControl is created dynamically at runtime, its events are not initially synchronized with those of other controls on the page. For more information, see the following section: Catch-up Events for Added Controls. Therefore, when file upload is performed on the Init event, it can cause code skipping.

Example

In this example, ASPxUploadControl is created in the Page_Load event handler. The control is partially initialized before it is added to the page hierarchy. After the control is added, it raises the Init event. Since the FileUploadMode property is set to BeforePageLoad (the default setting), file upload is performed on the Init event. After the upload is performed, the request execution is aborted and the following code is not executed on file upload.

csharp
protected void Page_Load(object sender, EventArgs e) {
     ASPxUploadControl uploadControl = new ASPxUploadControl();
     uploadControl.ID = "MyUploadControl";
     uploadControl.ShowUploadButton = true;
     form1.Controls.Add(uploadControl);
     //The code below is not executed during file upload
     uploadControl.FileUploadComplete += uploadControl_FileUploadComplete;

     ASPxButton btn = new ASPxButton();
     form1.Controls.Add(btn);
     btn.Text = "Click me";
     btn.Click += ASPxButton1_Click;
}
vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     Dim uploadControl As New ASPxUploadControl()
     uploadControl.ID = "MyUploadControl"
     uploadControl.ShowUploadButton = True
     form1.Controls.Add(uploadControl)
     'The code below is not executed during file upload
     uploadControl.FileUploadComplete += uploadControl_FileUploadComplete

     Dim btn As New ASPxButton()
     form1.Controls.Add(btn)
     btn.Text = "Click me"
     btn.Click += ASPxButton1_Click
End Sub

Set the FileUploadMode property to OnPageLoad to prevent the code from skipping during file upload. In this case, file upload is performed on the control’s Load event, after the Page_Load event handler has been fully processed.

csharp
protected void Page_Load(object sender, EventArgs e) {
     ASPxUploadControl uploadControl = new ASPxUploadControl();
     uploadControl.ID = "MyUploadControl";
     uploadControl.ShowUploadButton = true;
     uploadControl.FileUploadMode = UploadControlFileUploadMode.OnPageLoad;
     form1.Controls.Add(uploadControl);
     uploadControl.FileUploadComplete += uploadControl_FileUploadComplete;

     ASPxButton btn = new ASPxButton();
     form1.Controls.Add(btn);
     btn.Text = "Click me";
     btn.Click += ASPxButton1_Click;
}
vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     Dim uploadControl As New ASPxUploadControl()
     uploadControl.ID = "MyUploadControl"
     uploadControl.ShowUploadButton = True
     uploadControl.FileUploadMode = UploadControlFileUploadMode.OnPageLoad
     form1.Controls.Add(uploadControl)
     uploadControl.FileUploadComplete += uploadControl_FileUploadComplete

     Dim btn As New ASPxButton()
     form1.Controls.Add(btn)
     btn.Text = "Click me"
     btn.Click += ASPxButton1_Click
End Sub