Bind (Populate) ASP.Net Repeater in Master Page asynchronously (Async Method) using C#

Last Reply 11 months ago By EmadKhan

Posted 11 months ago

How to use async method in masterpage

I have a repeater in my master page which I want to bind asynchronously, please tell me how can I achieve this task.

referring to my thread :

Bind (Populate) ASP.Net GridView asynchronously (Async Method) using C#

Basically my login page is not linked with any master page, other child pages are inherited from master page.



Posted 11 months ago Modified on 11 months ago

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];

        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)

                    DataSet myDataSet = new DataSet();
                    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.