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>
3
  • 1
    why don't use ajax to get the value? Commented May 27, 2019 at 3:24
  • I'm not familiar to javascript, I will try ajax, thanks @Se0ng11 Commented May 27, 2019 at 3:28
  • 1
    Possible duplicate of Passing variable from ASP.net to JavaScript Commented May 27, 2019 at 3:53

1 Answer 1

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);
}

Sign up to request clarification or add additional context in comments.

1 Comment

error in dynamic xx = "<%=MyMethod%>"; Uncaught SyntaxError: Unexpected identifier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.