开发者

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("recommendationsHandl开发者_C百科er.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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜