简体   繁体   中英

String is undefined

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?!?!

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM