views:

143

answers:

3

I've embeded the following server side code within <script> tag.

<%  Dim dataTable As DataTable = cTab.getTabs(Session("UserID"))
    If (dataTable.Rows.Count > 0) Then
       For i As Int16 = 0 To dataTable.Rows.Count%>
       {
           contentEl: 'tab'+'<%dataTable.Rows(0)("TabID").ToString()%>', 
           title: '<%dataTable.Rows(0)("TabName").ToString()%>',
           closable: false,
           autoScroll: true
           },
        <% Next
    End If %>

But it is not returning the desired results due to syntax problems. How can I write it correctly?

A: 

You've only given us a portion of the entire javascript, so we can't analyze what the surrounding script looks like in order to look for syntax issues. The snippet you've given will just start with an opening curly brace, which is not valid just standing by itself. What's preceeding it?

Besides that, if you want to output a string to the script, you need to do it using Response.Write or using the <%= %> shortcut. So for one, you need to do:

   contentEl: 'tab'+'<%= dataTable.Rows(0)("TabID").ToString() %>', 
   title: '<%= dataTable.Rows(0)("TabName").ToString() %>',

(Notice the = signs).

womp
A: 

Assuming your loop is working you probably don't want to have the same value every time. I think you might want to use i instead of 0. That is just a wild guess though since I really can't tell if the loop should work or not.

contentEl: 'tab'+'<%= dataTable.Rows(i)("TabID").ToString()%>', 
Jason Rowe
A: 

Without seeing the surrounding code....

Looking at the loop each item is generated with a trailing comma (,) this means that there will be an extra comma at the end which is not valid is some browsers.

Invalid Example

var myObjArr = [
  {
    contentEl: 'tab'+'123', 
    title: 'name123',
    closable: false,
    autoScroll: true
  },
  {
    contentEl: 'tab'+'456', 
    title: 'name456',
    closable: false,
    autoScroll: true
  },
];

Valid Example

var myObjArr = [
  {
    contentEl: 'tab'+'123', 
    title: 'name123',
    closable: false,
    autoScroll: true
  },
  {
    contentEl: 'tab'+'456', 
    title: 'name456',
    closable: false,
    autoScroll: true
  }
];

Note the second sample has no extra comma (,).

Jonathan