views:

96

answers:

1

I've got the following property in the code-behind of my .aspx:

    protected string CurrentProductView
    {
        get
        {
            string viewName = string.Empty;
            viewName = Enum.GetName(typeof(ProductView), currentProdView);
            return viewName;
        }
    }

In my .aspx I've got some Javascript that tries to reference this string:

$(document).ready(function ()
{
    var action = <%=CurrentProductView %>

             $("#recommendations").load("recommendationsHandler.ashx?action=" + action + "item&csid=" + csid + "&productID=" + productID, function()
        {
            $("#recommendationsView").show();
        });
});

but for some reason I get "Item is not defined".

When I debug this, I'm definitely seeing a string come back for viewName. So why would it complain if a string is coming back?!?!

+5  A: 

Change this:

var action = <%=CurrentProductView %>

to this:

var action = "<%=CurrentProductView %>"

Since you are printing out a string value to the page into a variable, you need quotes around it, because the value is viewed as a literal value to the JavaScript on the page. You don't need the quotes around ints, because in JavaScript this is legal:

var my_number = 4;

where this is not

var my_string = this is a string;

and it needs to be this:

var my_string = "this is a string";
Kevin
I guess I don't understand why...because it's jQuery? Because we've defined other variables of type int like this: var productID = <%=ProductID %>;
CoffeeAddict
because product id is an int and in JavaScript, ints don't need quotes around them to be defined.
Kevin
That't what I was thinking, but I'm not familiar enough with aspx to say for sure. (Maybe string values are automagically quoted, for example.)
JSBangs
So by putting quotes around it, isn't it no longer going to reference my server-side property and now it's a string literal?
CoffeeAddict
I just figured that if I quote this, it's going to treat it literally...not replace the <% %> with the actual value of the server-side property
CoffeeAddict
Nope, it will replace the value, because the server side processing will recognize the server tags and replace the value. Now if you did something like <%="MY_Value"%> that's something different.
Kevin
@coffeeaddict, it'll be literal to *javascript* but not to *asp*, which will interpolate your <% %> values even inside a javascript string.
JSBangs
yea, I know what it does but I thought that adding "" around that whole thing then setting it to a var would show literal to javascript and not the server-side value that replaces that stub <% %>
CoffeeAddict
well it does not work still. When that url gets to my handler page, action is null and I know for sure that server-side property has a valid string value.
CoffeeAddict
Honestly I guess I don't understand the sequence then. So server-side is called, it sets the property, and then once the page renders replaces that literal with the value that my server-side code contains? I guess then if JavaScript is treating that as a literal, how is my servers-side property hooking up then with that literal meaning when is that string referenced server-side then transformed into the actual value. My mislink is HOW this all happens between the server-side and javascript.
CoffeeAddict
It's working, I used single quotes around it (probably doesn't matter if it's single or double...). But to answer the How, is what is really confusing to me. How it's interpolated and when on the loading of the page when you look at the server-side process and client-side.
CoffeeAddict