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

Last Reply 27 days ago By EmadKhan

Posted one month 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.

Thanks.

 

You are viewing reply posted by: EmadKhan 27 days ago.
Posted 27 days ago Modified on 27 days 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];
            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.