c# - Asynchronously populate gridview on button click -


i populate gridview inside update panel on button click. gridview getting populated doesn't show on screen. missing? below code i'm using:

  public delegate void bindgrid_delegate();   protected void btnsearch_click(object sender, eventargs e)   {         try         {             // databind of controls             bindgrid_delegate bd = new  bindgrid_delegate(bindgrid);             iasyncresult ar = bd.begininvoke(null, null); //invoking method         }         catch (exception ex)         {             scriptmanager.registerclientscriptblock(this, this.gettype(), "pageexception", "alert('" + ex.message + "');", true);         }   }    private void bindgrid()   {       try       {           dataset resultdataset = getdata();           gvshowresult.datasource = resultdataset;            gvshowresult.databind();           updatepanel2.update();       }       catch (exception ex)       {           scriptmanager.registerclientscriptblock(this, this.gettype(), "pageexception", "alert('" + ex.message + "');", true);       }   } 

updatemode updatepanel conditional. i'd appreciate help.

the problem call databind() , update() on non-ui thread. ui controls should modified on thread creates them.

you can still call getdata() method in seperate thread, e.g. using tasks.

in btnsearch_click method:

task.factory     .startnew(() => getdata())     .continuewith(t =>      {         gvshowresult.datasource = t.result;          gvshowresult.databind();         updatepanel2.update();     }, taskscheduler.fromcurrentsynchronizationcontext()); 

this calls getdata() on seperate thread , executes continuation on ui thread.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -