0

I want to send a variable from C# to Javascript dynamic.

I have tried the following code down below.

But the method or the variable will only read once.

How can I make it work ?

in aspx.cs

    public partial class WebForm2 : System.Web.UI.Page
    {
        internal static string timer = "false";

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public string MyMethod()
        {
            return timer;
        }
    }

in aspx

<script>
    function GetMyName()
    {
        setInterval(function() {
            dynamic xx = "<%=MyMethod()%>";
            console.log(xx);
        }, 1000);
    }
</script>
Kevin Lee
  • 31
  • 5

1 Answers1

0

Just create a property for your MyMethod()

protected string MyMethod { get { return timer; } }

Then in JS

<script>
function GetMyName()
{
    setInterval(function() {
        dynamic xx = "<%=MyMethod%>";
        console.log(xx);
    }, 1000);
}

MaMy
  • 1
  • 4