0

I am trying to read a C# property in JavaScript (not using Ajax). In C#, I am setting property on page load. I tried to read this property like so:

<script type="text/javascript">
    var ProductId =<%=this.ProductId %>>
    alert(ProductId);   // not successful alert showed undefiend

    function GetValueNow()
    {
        alert(<%=this.ProductId%>); // calling this function was showing value
    }
</script>

I tried to access this property in on page load (in the JavaScript of the .aspx page) but was not successful. Later, I tried to do this in a JavaScript function, and that worked.

Why can't I read the variable before the body of GetValueNow()?

2 Answers 2

5

You've got an extra > sign:

From:

var ProductId =<%=this.ProductId %>>

To:

var ProductId = <%=this.ProductId %>;
Sign up to request clarification or add additional context in comments.

Comments

3

Looks like it is just a typo.

    var ProductId =<%=this.ProductId %>>

should be:

    var ProductId =<%=this.ProductId %>;

Comments

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.