asp.net - C# Delegate, Label text not changing -


i have created delegate each time click button text within label's text should change reason not work , label's text not change.

this aspx page:

<body>     <form id="form1" runat="server">     <div>         <asp:button id="btnfeed" onclick="btnfeed_click" runat="server" text="button" />         <asp:label id="lblraceresults" runat="server" text="label"></asp:label>     </div>     </form> </body> 

this aspx.cs page

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols;  namespace webprogramming3.week_3 {     public partial class exercise1 : system.web.ui.page     {         //only testing         static person test_person;         static person person2;         static person person3;          protected void page_load(object sender, eventargs e)         {             if (!ispostback)             {                 test_person = new person("neil");                 person2 = new person("2");                 person3 = new person("3");                 test_person.onfullyfed += test_person_onfullyfed;                 person2.onfullyfed += test_person_onfullyfed;                 person3.onfullyfed += test_person_onfullyfed;             }         }          private void test_person_onfullyfed(string message)         {             // httpcontext.current.response.write(message + " full");             lblraceresults.text = message; //<--this label text not change         }          protected void btnfeed_click(object sender, eventargs e)         {             test_person.feed(1);             person2.feed(2);             person3.feed(3);         }     }      public delegate void stringdelegate(string message);      public class person     {         public string name { get; set; }         public int hunger { get; set; }          public event stringdelegate onfullyfed;          public person(string name)         {             name = name;             hunger = 3;         }          public void feed(int amount)         {             if(hunger > 0)             {                 hunger -= amount;                 if(hunger <= 0)                 {                     hunger = 0;                      //this person full, raise event                     if (onfullyfed != null)                         onfullyfed(name);                 }             }         }      } } 

i sure delegate coded correctly when uncomment line

httpcontext.current.response.write(message + " full"); 

i message each time click button

this because page life-cycle has finished , page rendered / sent browser before threads have finished updating controls. during debug can see threads completing work altering labels have been sent browser.

removing !ispostback load event should trick , reload controls. of course there other options use resolve this, having update panel , auto refresh.

 protected void page_load(object sender, eventargs e)         {                 test_person = new person("neil");                 person2 = new person("2");                 person3 = new person("3");                 test_person.onfullyfed += test_person_onfullyfed;                 person2.onfullyfed += test_person_onfullyfed;                 person3.onfullyfed += test_person_onfullyfed;          } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -