c# - Change the text of label from javascript -


so first problem happen when click button? page refresh or else, because i'm working on small project , facing problem

<script language="javascript" type="text/javascript">     var totaltime = 0;     var interval = 0;     var min = 0;     var hr = 0;     var sec = 0;     var secstr = 0;     var minstr = 0;     var flag = 0;     window.onload = startresettimer      function startresettimer() {         totaltime = 20;         hr = math.floor(totaltime / 3600);         sec = totaltime % 60;         if (sec == 0)             min = (totaltime / 60) % 60;         flag = 1;         interval = setinterval(function () {             if (flag == 1) {                 if (totaltime <= 0) {                     flag = 0;                     pagemethods.callit();                 }                 else {                     totaltime--;                     hr = math.floor(totaltime / 3600)                     sec = totaltime % 60                     if (sec == 0)                         min = (totaltime / 60) % 60                     if (sec < 10)                         secstr = "0" + sec                     else                         secstr = sec                     if (min < 10)                         minstr = "0" + min                     else                         minstr = min                 }             }             console.log(document.getelementbyid('maincontent_label2'));             document.getelementbyid('maincontent_label2').innerhtml = "" + hr + ":" + minstr + ":" + secstr         }, 1000)     }  </script> 

this timer code , when timer expire i'm calling page method "callit()" //just debug name// //asp.net code

public partial class _default : system.web.ui.page {     public static _default obj;     protected void page_load(object sender, eventargs e)     {         obj = this;     }     public void print()     {         label1.text = "called form every where";     }     [webmethod]     public static void callit()     {         obj.print();     }      protected void button1_click(object sender, eventargs e)     {         obj.print();     } } 

now callit() call function print() change text of "label1" , calling same function print() button click, when press button change text of label1 when timer expire doesn't, print() function s called check using breakpoints doesn't reflect in page.so thought page refresh should place

if(totaltime <= 0) {    pagemethods.callit();    window.location.reload() ; } 

but restart timer.

when call web method of asp.net page javascript, ms javasvript engine ajax request page , return javascript data provided service call , that's it. callit method returns nothing in javascript receive nothing, , said else not evaluated, please not store "this" in static property, 'cause page object recreates each time during each request , can wrong in in multi-client environment.

so recommend call method javascript, collect data method returns , manually update label using javascript in browser


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -