user576510
user576510

Reputation: 5905

Why can't JavaScript read this C# variable?

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()?

Upvotes: 0

Views: 193

Answers (2)

Jonathan Fingland
Jonathan Fingland

Reputation: 57167

Looks like it is just a typo.

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

should be:

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

Upvotes: 3

Alex
Alex

Reputation: 35409

You've got an extra > sign:

From:

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

To:

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

Upvotes: 5

Related Questions