We can use async in master page, please find the code below.
private string connection = ConfigurationManager.ConnectionStrings["xxxxx"].ConnectionString.ToString();
public async Task GetSomeData()
{
//Use Async method to get data.
DataSet results = await GetDataSetAsync(connection, "Select * from notifications", null);
//Populate once data received.
AllNotifications.DataSource = results.Tables[0];
AllNotifications.DataBind();
}
public Task<DataSet> GetDataSetAsync(string sConnectionString, string sSQL, params SqlParameter[] parameters)
{
return Task.Run(() =>
{
using (SqlConnection newConnection = new SqlConnection(sConnectionString))
using (SqlDataAdapter mySQLAdapter = new SqlDataAdapter(sSQL, newConnection))
{
mySQLAdapter.SelectCommand.CommandType = CommandType.Text;
if (parameters != null)
{
mySQLAdapter.SelectCommand.Parameters.AddRange(parameters);
}
DataSet myDataSet = new DataSet();
mySQLAdapter.Fill(myDataSet);
return myDataSet;
}
});
}
and in page_load of masterpage:
if (!IsPostBack)
{
Page.RegisterAsyncTask(new PageAsyncTask(GetSomeData));
}
Note: We have to use async="true" in all child pages for this code to work.