开发者

String from codebehind to array in Javascript

Hi all i have code that reads from a DB and populates a string in the code behind

List<string> rows = new List<string>();
    DataTable prods = common.GetDataTable("vStoreProduct", new string[] { "stpt_Name" }, "stpt_CompanyId = " + company.CompanyId.ToString() + " AN开发者_开发百科D stpt_Deleted is null");

    foreach (DataRow row in prods.Rows)
    {

        prodNames += "\"" + row["stpt_Name"].ToString().Trim() + "\",";
    }
    string cleanedNanes =  prodNames.Substring(0, prodNames.Length - 1);
    prodNames = "[" + cleanedNanes + "]";

This produces something like ["Test1","Test2"]

In javascript i have

var availableTags = '<% =prodNames %>';

alert(availableTags);

How can i access this like an array in javascript like

alert(availableTags[5]);

and get the full item at the given index.

Thanks any help would be great


Get rid of the quotes:

var availableTags = <% =prodNames %>;

With the quotes there, you're creating a JavaScript string. Without them, you've got a JavaScript array constant.


You're going to have to split the variable from .NET into a JS array.

Check out: http://www.w3schools.com/jsref/jsref_split.asp

Example based on your code:

var availableTags = '<% =prodNames %>';
var mySplitResult = availableTags .split(",");
alert(mySplitResult[1]);


I believe split() will do what you want:

    var availableTagsResult = availableTags.split(",");
    alert(availableTagsResult[1]) //Display element 1

This will create an array from the string which has been split on ,

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜