Populate a combo with an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lukelrc

    Populate a combo with an array

    Hi,

    I have an array - ListOfFiles - that i want use to populate an combo.

    I've attempted to do it like this

    <select name="txtAvaila ble" rows="4" id="txtAvailabl e">
    <Script Language = "vbscript" Runat = "Server">
    For i = 0 to Count -1
    Response.Write" <OPTION>" & ListOfFiles(i) & "</OPTION>"
    next
    </script></select>

    I placed the vbscript in the approprate place in the body. But when i
    open the page instead of writing the HTML where i placed the code, it
    has appended it right at the bottom of the page. Does anyone know
    where i'm going wrong or if there's a better way of doing this?

    Thanks

    Luke
  • Ray at

    #2
    Re: Populate a combo with an array

    When things appear on the page where you don't expect them to, this is often
    because of some poorly written HTML, not code. Example:

    <table border="1">
    <tr>
    <td>XXX</td>
    YYY
    </tr>
    </table>

    The YYY will appear at the top of the page (in IE, anyway), although one may
    expect it to appear in the table.

    Basically, if you're seeing your select with the options in your array, your
    ASP code isn't the problem.

    Ray at work

    "Lukelrc" <luke.curtis@we stoxon.gov.uk> wrote in message
    news:201051d6.0 405190832.1cfda 517@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > I have an array - ListOfFiles - that i want use to populate an combo.
    >
    > I've attempted to do it like this
    >
    > <select name="txtAvaila ble" rows="4" id="txtAvailabl e">
    > <Script Language = "vbscript" Runat = "Server">
    > For i = 0 to Count -1
    > Response.Write" <OPTION>" & ListOfFiles(i) & "</OPTION>"
    > next
    > </script></select>
    >
    > I placed the vbscript in the approprate place in the body. But when i
    > open the page instead of writing the HTML where i placed the code, it
    > has appended it right at the bottom of the page. Does anyone know
    > where i'm going wrong or if there's a better way of doing this?
    >
    > Thanks
    >
    > Luke[/color]


    Comment

    • Dave Anderson

      #3
      Re: Populate a combo with an array

      Lukelrc wrote:[color=blue]
      >
      > <select name="txtAvaila ble" rows="4" id="txtAvailabl e">
      > <Script Language = "vbscript" Runat = "Server">
      > For i = 0 to Count -1
      > Response.Write" <OPTION>" & ListOfFiles(i) & "</OPTION>"
      > next
      > </script></select>
      >
      > I placed the vbscript in the approprate place in the body. But when i
      > open the page instead of writing the HTML where i placed the code, it
      > has appended it right at the bottom of the page. Does anyone know
      > where i'm going wrong or if there's a better way of doing this?[/color]

      This is an order-of-execution issue (http://aspfaq.com/show.asp?id=2045). If
      you want to interlace server-side scripting with HTML, you will need <% %>
      blocks.

      I usually abstract as much processing as possible before writing any HTML.
      In your case, I would build a single string to represent the entire array...

      FilenameOptionL ist = CreateOptions(L istOfFiles,"")

      ....then insert it inline:

      <SELECT><%=File nameOptionList% ></SELECT>

      You can define the function anywhere in the script, either in <% %> blocks
      or in <SCRIPT RUNAT="Server"> blocks. Here is a sample function:

      =============== =============== =============== =============== ============
      Function CreateOptions(b yVal A(), byVal DefaultValue)
      Dim i, val
      For i = 0 To UBound(A)
      val = Server.HTMLEnco de(A(i))
      If A(i) = DefaultValue Then
      A(i) = "<OPTION VALUE=""" & val & """ SELECTED>" & _
      val & "</OPTION>"
      Else
      A(i) = "<OPTION VALUE=""" & val & """>" & _
      val & "</OPTION>"
      End If
      Next
      CreateOptions = Join(A,vbCrLf)
      End Function
      ------------------------------------------------------------------------

      Note that this example is capable of preselecting the control:

      ... = CreateOptions(L istOfFiles,Requ est.Form("txtAv ailable").Item)


      Lastly, it bears mentioning that in many cases, this abstraction allows you
      to simply use <SELECT><%=Crea teOptions(...)% ></SELECT>

      More complicated functions can be written to create OPTION lists from 2D
      arrays, wherein the .value and .text properties differ (<OPTION
      VALUE="TN">Tenn essee</OPTION>). Either way, this type of function is
      entirely self-contained, so it can be placed into a common include for reuse
      wherever needed.

      If you use JScript on the server side, you can even do something like
      this...

      Array.prototype .toOptions = function(val) {
      for (var i=0,a=[]; i<this.length; i++)
      a[i] = "\r\n<OPTIO N VALUE=\"" + this[i] +
      (this[i]==val ? "\" SELECTED>" : "\">") +
      this[i] + "</OPTION>"
      return a.join("")
      }

      ....in which case:

      <%=ListOfFiles. toOptions()%>

      would suffice, but:

      <%=ListOfFiles. toOptions(Reque st.Form("txtAva ilable").Item)% >

      works even better, and doesn't require a different method signature.


      --
      Dave Anderson

      Unsolicited commercial email will be read at a cost of $500 per message. Use
      of this email address implies consent to these terms. Please do not contact
      me directly or ask me to contact you directly for assistance. If your
      question is worth asking, it's worth posting.


      Comment

      • Dave Anderson

        #4
        Re: Populate a combo with an array

        Ray at <%=sLocation% > [MVP] wrote:[color=blue]
        > When things appear on the page where you don't expect them to, this
        > is often because of some poorly written HTML, not code. Example:
        >
        > <table border="1">
        > <tr>
        > <td>XXX</td>
        > YYY
        > </tr>
        > </table>
        >
        > The YYY will appear at the top of the page (in IE, anyway), although
        > one may expect it to appear in the table.
        >
        > Basically, if you're seeing your select with the options in your
        > array, your ASP code isn't the problem.[/color]

        That's not correct, Ray. This is clearly an execution order problem. Note
        the RUNAT="server" bit. If the default language is VBScript, that block
        executes last, no matter where it sits in your code.

        The fix is either abstraction or <%%> blocks.



        --
        Dave Anderson

        Unsolicited commercial email will be read at a cost of $500 per message. Use
        of this email address implies consent to these terms. Please do not contact
        me directly or ask me to contact you directly for assistance. If your
        question is worth asking, it's worth posting.


        Comment

        • Ray at

          #5
          Re: Populate a combo with an array


          "Dave Anderson" <GTSPXOESSGOQ@s pammotel.com> wrote in message
          news:ud3HavcPEH A.988@tk2msftng p13.phx.gbl...[color=blue][color=green]
          > > Basically, if you're seeing your select with the options in your
          > > array, your ASP code isn't the problem.[/color]
          >
          > That's not correct, Ray. This is clearly an execution order problem. Note
          > the RUNAT="server" bit.[/color]

          Oh yeah. Thanks Dave. Studying FAQ 2045 again now. :]

          Ray at work


          Comment

          • Luke Curtis

            #6
            Re: Populate a combo with an array

            Thanks for all your help.

            I have created a string as suggested FileNameoptionl ist, which is fine.

            Ive checked out about the execution order, but i cant see what exactly
            the problem is. What is strange is that if i script:

            <script language="vbscr ipt"
            Runat="server"> Response.write( filenameOptionL ist)</script>

            in either the head or the body, it produces the correct HTML but still
            apended at the end - which does suggest an execution order problem, but
            if i simply write:

            <% response.write( Filenameoptionl ist)%>

            or

            <% =filenameoption list%>

            I get nothing at all!

            Does anyone know why this might be?
            Thanks in advance.

            Luke



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Lukelrc

              #7
              Re: Populate a combo with an array

              Thanks for all your help.

              I have created a string as suggested FileNameoptionl ist, which is fine.

              Ive checked out about the execution order, but i cant see what exactly
              the problem is. What is strange is that if i script:

              <script language="vbscr ipt"
              Runat="server"> Response.write( filenameOptionL ist)</script>

              in either the head or the body, it produces the correct HTML but still
              apended at the end - which does suggest an execution order problem, but
              if i simply write:

              <% response.write( Filenameoptionl ist)%>

              or

              <% =filenameoption list%>

              I get nothing at all!

              Does anyone know why this might be?
              Thanks in advance.

              Luke

              Comment

              • Ray at

                #8
                Re: Populate a combo with an array

                What about:

                <% Response.WRite "Here is the filenameoptions list: " & Filenameoptionl ist
                %>

                Ray at work

                "Lukelrc" <luke.curtis@we stoxon.gov.uk> wrote in message
                news:201051d6.0 405200417.49e97 7a1@posting.goo gle.com...[color=blue]
                > Thanks for all your help.
                >
                > I have created a string as suggested FileNameoptionl ist, which is fine.
                >
                > Ive checked out about the execution order, but i cant see what exactly
                > the problem is. What is strange is that if i script:
                >
                > <script language="vbscr ipt"
                > Runat="server"> Response.write( filenameOptionL ist)</script>
                >
                > in either the head or the body, it produces the correct HTML but still
                > apended at the end - which does suggest an execution order problem, but
                > if i simply write:
                >
                > <% response.write( Filenameoptionl ist)%>
                >
                > or
                >
                > <% =filenameoption list%>
                >
                > I get nothing at all!
                >
                > Does anyone know why this might be?
                > Thanks in advance.
                >
                > Luke[/color]


                Comment

                • Roland Hall

                  #9
                  Re: Populate a combo with an array

                  "Lukelrc" wrote in message
                  news:201051d6.0 405190832.1cfda 517@posting.goo gle.com...
                  : I have an array - ListOfFiles - that i want use to populate an combo.
                  : I've attempted to do it like this
                  :
                  : <select name="txtAvaila ble" rows="4" id="txtAvailabl e">
                  : <Script Language = "vbscript" Runat = "Server">
                  : For i = 0 to Count -1
                  : Response.Write" <OPTION>" & ListOfFiles(i) & "</OPTION>"
                  : next
                  : </script></select>
                  :

                  Untested, just written here...

                  <%
                  Dim Count
                  Count = ubound(ListOfFi les())

                  sub prt(str)
                  Response.Write( str & vbCrLf)
                  end sub

                  sub lprt(str)
                  Response.Write( str & "<br />" & vbCrLf)
                  end sub

                  sub popList()
                  prt("<select name=""txtAvail able"" rows=""4"" id=""txtAvailab le"">")
                  for i = 0 to Count - 1
                  prt("<option value=""" & ListOfFiles(i) & """>" & ListOfFiles(i) &
                  "</option>")
                  next
                  prt("</script>")
                  end sub

                  lprt("Check out my list of files...")
                  popList
                  %>

                  HTH...

                  --
                  Roland Hall
                  /* This information is distributed in the hope that it will be useful, but
                  without any warranty; without even the implied warranty of merchantability
                  or fitness for a particular purpose. */
                  Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
                  WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
                  MSDN Library - http://msdn.microsoft.com/library/default.asp


                  Comment

                  • Lukelrc

                    #10
                    Re: Populate a combo with an array

                    I've sorted it. When i had written the script that populated the
                    string i had open and closed script tags several times. Putting all of
                    the code into one set of tags sorted it. Thanks for your help.



                    Luke

                    Comment

                    • Roland Hall

                      #11
                      Re: Populate a combo with an array

                      "Lukelrc" wrote in message
                      news:201051d6.0 405210136.782dc 316@posting.goo gle.com...
                      : I've sorted it. When i had written the script that populated the
                      : string i had open and closed script tags several times. Putting all of
                      : the code into one set of tags sorted it. Thanks for your help.

                      Glad to hear it's working for you Luke. (O:=


                      Comment

                      Working...