0

I have datarow written from the database.how can I access the list in javascript? Please help.

  StringBuilder sb = new StringBuilder();
        sb.Append("<script>");
        sb.Append("var testArray = new Array;");
        foreach (DataRow str in result)
        {
            sb.Append("testArray.push('" + str.ItemArray[1].ToString() + "');");
        }
        sb.Append("</script>");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "test('"PassTestArrayHere"');", true);
4
  • Never heard of json? james.newtonking.com/projects/json-net.aspx Commented Jul 9, 2013 at 17:42
  • Check out this SO answer: stackoverflow.com/questions/11826936/… Commented Jul 9, 2013 at 17:43
  • One of two ways. Either 1) render the data to the page when the page loads, or; 2) make an AJAX call to the server from the page to get the data. The first option is a lot easier unless you have a compelling reason to use the latter option. What exactly is this code trying to do, anyway? What does the rendered JavaScript look like and in what way does it fail? Commented Jul 9, 2013 at 17:43
  • its failing because of the script tag! I have method that collects data from database and return list and I want to pass it to javascript since i m not using AJAX. Commented Jul 9, 2013 at 18:17

1 Answer 1

1

Your script will not work, instead of using sb.Append("testArray.push('" + str.ItemArray[1].ToString() + "');"); you can convert you array to JSON and return to client. In case you still want to follow you approach, you should convert your array to a string with the format like: String a = "['item0', 'item1', 'item2']"; and then:

Page.ClientScript.RegisterStartupScript(String.GetType(), "test", "test(" + a + ");", true);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.