View Single Post
  #18 (permalink)  
Old May 8th, 2008, 12:18 PM
gok gok is offline
Registered User
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Still can not put html <-> asp pair to work.
Did create inside ASP (server-side) a hidden list in
var names=["a","b"]; form. But in HTM (client-side)
got runtime error: 'names' is undefined.
Switched example to pub dbase in MSSQL so everybody
can try it.

Is it possible to bring 'names' in .htm?

<%@ LANGUAGE=JavaScript %>
    Response.Write("<script type='text/javascript' language='javascript'>\n");

    var conn = Server.CreateObject("ADODB.Connection");
    var strConn = "Provider=SQLOLEDB.1;Data source=amsql;Database=pubs;Trusted_Connection=no;User ID=guest;Password=password;"
    var rs = conn.Execute("SELECT * FROM STORES");

    var i=0;
    Response.Write("var names=[");
    while (!rs.EOF)
        if ( i++>0 ) Response.Write(",");
        var name = rs("stor_name");
        // Use a regular expression to replace leading and trailing
           // spaces with the empty string
//           name.replace(/(^\s*)|(\s*$)/g, ""); // Object doesn't support this property or method
        Response.Write("\"" + name + "\"");
    Response.Write("</s" + "cript>");
<%@ LANGUAGE=JavaScript %>
<SCRIPT LANGUAGE="JavaScript" src="readDb.asp"></SCRIPT>
Store Names:<br>
    <select name="quickZoom" size="5">
        <option selected value="-">zoom to...
<%            for ( i = 0; i < names.length; ++i )  %>
                <option value="<%=names[i]%>"><%=names[i]%>
quote:Originally posted by joefawcett
 Firstly, as I said before, you must be clear in your mind which code executes on the server and which on the client. When you return names from the server-side code it doesn't go anywhere. You need to create the HTML that you need:
  • Code:
    var names = readDB();
    Response.Write("<script type=\"text/javascript\">\n");
    Response.Write(" var names = " + names.toString() + ";\n");
    Response.Write("</s" + "cript>\n");

  • In your client-side code don't use document.writeln, create a page as you wish with an empty select element and use the DOM to create new Option elements and add them based on the data in the names variable
  • Check the HTMl of the page created to see whether the array has been populated correctly, it should look like:
    var names= ["name1", "name2", "name3"];


Joe (Microsoft MVP - XML)
Reply With Quote