开发者

String is undefined

开发者 https://www.devze.com 2023-01-04 19:54 出处:网络
I\'ve got the following property in the code-behind of my .aspx: protected string CurrentProductView { get

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

精彩评论

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