Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dears

I have a Gridview and it contain a teamplate link button column .The link button click evnets take a long time process .

In my code i using a Thread to manage the long time process. But if the thread complete the process the GridView is not updating it's status. How i update the gridview link button text after the thread execute ? .

I tried to re-bind the gridview but it throw this error script controls may not be registered after prerender

My code as follows

The following code is working fine and the only problem is that after completing each thread i calling<br />
 a method called "PTACalculationEnd"  and in this method i updating link button grid template column text will be changed to "Already Calculated"   But.. it is not updating the Grid view and  no error .



Grid view Itemcommand event


protected void GrdPTAConfigure_ItemCommand(object sender, GridCommandEventArgs e)
       {

           GridDataItem item = (GridDataItem)e.Item;
           LinkButton button = item["tclnkConfigure"].Controls[0] as LinkButton;
           button.Text = "Calculating...";
           button.Font.Underline = false;
           button.ForeColor = System.Drawing.Color.LightSeaGreen;
           button.Enabled = false;

           ((e.Item as GridDataItem)["chkSelectPTA"].Controls[0] as CheckBox).Enabled = false;

           Thread t = new Thread( new ThreadStart(delegate()
           {
               DoPTACalculations(e, PTACalculationEnd);
           }));

           t.Start();


       }




private void DoPTACalculations(GridCommandEventArgs e, Callback callback)
       {


           if (e.CommandName == "Calculate")
           {
              //Do a Long Process Task

            }
        }



public delegate void Callback();

      public void PTACalculationEnd()
      {
              LinkButton button = item["tclnkConfigure"].Controls[0] as LinkButton;

              if (IsAlreadyConfigured(ddlBranch.SelectedValue.ToString())
              {

                  button.Text = "Already Calaculated";
                  button.Font.Underline = false;
                  button.ForeColor = System.Drawing.Color.Green;
                  button.Enabled = true;

              }

              else
              {

                  button.Text = "Calculate";
                  button.Font.Underline = true;
                  button.ForeColor = System.Drawing.ColorTranslator.FromHtml("#0060d0");
                  button.Enabled = true;
              }

          }



      }
Posted
Updated 15-Jul-12 2:25am
v2

1 solution

Try changing this

C#
Thread t = new Thread( new ThreadStart(delegate()
            {
                DoPTACalculations(e, PTACalculationEnd);
            }));

            t.Start();


to this


DoPTACalculations(e, PTACalculationEnd);


In the sample you show you do not have any mechanism for the page to determine when your thread has completed. I imagine that the server sends the response before your thread has finished.

Edit:
Per your comment, if you require long processing thread to handle this then you need to manage the threads so the response does not end before you are done trying to modify the output. I suggested that just so you would be able to see that it is not showing the changes you expected because the server sends the page response while your threads are still executing. I would imaging that when your page reloads after triggering the ItemCommand your button's text says "Calculating..." and is disabled. Once the response is sent back to the client there is no way for the thread to send out the changes it trys to make.

Here are some links you should check out

Multi-Threading in ASP.NET[^]

Use the Asynchronous Power of ASP.NET to Create a Simple, Reusable Page for Displaying a Processing Message[^]
 
Share this answer
 
v2
Comments
Riyasktr 16-Jul-12 2:15am    
If i using as per your answer ( without threading) it is not possible to manage the long time process and i can;t do the next process till y first process will be completed.

In my scenario the thread is completed it automatically call the method "PTACalculationEnd"

Thread t = new Thread( new ThreadStart(delegate()
{
DoPTACalculations(e, PTACalculationEnd);
}));

t.Start();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900