I am trying to find a way to perform an asynchronous method in the Page_Load method of a webforms page, without blocking the page load for the user. I am using the following code:-
C# Code behind
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterAsyncTask(new PageAsyncTask(AsyncDatabind));
}
private async Task AsyncDatabind()
{
await Task.Delay(5000);
Test.Text = "Hello!";
}
}
ASPX view
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AsyncTest._Default" Async="true"%>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="Test" runat="server"></asp:Label>
</asp:Content>
Ideally, the page should load for the user, then after the async task completes, the label is updated. So the page should be visible for 5 seconds before the label text is set. Is this possible using PageAsyncTask?