Dynamic drop down list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mcgrew.michael@gmail.com

    Dynamic drop down list

    I hope this is the right group. I am very new to ASP so this is
    probably a stupid question. I have some vbscript that query's AD and
    populates a recordset. I know the recorset contains the information I
    want by doing a Response.write. I am having problems dynamically
    creating a drop down list from the data in the recordset. The drop down
    is created but it is empty. Any help would be greatly appreciated. A
    sample of the code:

    <%
    'On Error Resume Next
    Set objRootDSE = GetObject("LDAP ://RootDSE")
    strDNSDomain = objRootDSE.Get( "defaultNamingC ontext")
    strOU = "OU=Generic Accounts,"
    ' Use ADO to search Active Directory.
    Set objCommand = CreateObject("A DODB.Command")
    Set objConnection = CreateObject("A DODB.Connection ")
    objConnection.P rovider = "ADsDSOObje ct"
    objConnection.O pen "Active Directory Provider"
    objCommand.Acti veConnection = objConnection
    strBase = "<LDAP://" & strOU &" " & strDNSDomain &">"
    strFilter = "(&(objectCateg ory=person)(obj ectClass=user)) "
    strQuery = strBase & ";" & strFilter & ";cn;subtre e"
    objCommand.Comm andText = strQuery
    objCommand.Prop erties("Page Size") = 100
    objCommand.Prop erties("Timeout ") = 30
    objCommand.Prop erties("Cache Results") = False
    Set objRecordSet = objCommand.Exec ute
    objRecordSet.Mo veFirst
    Do Until objRecordSet.EO F
    sName = objRecordSet.Fi elds("cn").Valu e
    'Response.Write (sName)
    genericAccount = "<option value= & '"sName"' & >"

    objRecordSet.Mo veNext
    Loop
    %>
    <html><head><ti tle>Dynamic Drop-Down Menu Example</title></head>
    <body>
    <form method="POST" action="test3.a sp">
    <p><select size="1" name="GenericAc counts">
    <%=genericAccou nt%>
    </select></p>
    </form>
    </body>
    </html>

  • Michael Kujawa

    #2
    Re: Dynamic drop down list

    genericAccount = "<option value= & '"sName"' & >"

    I would think should be

    genericAccount = genericAccount & "<option value='" & sName & "'>" & sName &
    "</option>"



    <mcgrew.michael @gmail.comwrote in message
    news:1156791520 .826402.266330@ 74g2000cwt.goog legroups.com...
    I hope this is the right group. I am very new to ASP so this is
    probably a stupid question. I have some vbscript that query's AD and
    populates a recordset. I know the recorset contains the information I
    want by doing a Response.write. I am having problems dynamically
    creating a drop down list from the data in the recordset. The drop down
    is created but it is empty. Any help would be greatly appreciated. A
    sample of the code:
    >
    <%
    'On Error Resume Next
    Set objRootDSE = GetObject("LDAP ://RootDSE")
    strDNSDomain = objRootDSE.Get( "defaultNamingC ontext")
    strOU = "OU=Generic Accounts,"
    ' Use ADO to search Active Directory.
    Set objCommand = CreateObject("A DODB.Command")
    Set objConnection = CreateObject("A DODB.Connection ")
    objConnection.P rovider = "ADsDSOObje ct"
    objConnection.O pen "Active Directory Provider"
    objCommand.Acti veConnection = objConnection
    strBase = "<LDAP://" & strOU &" " & strDNSDomain &">"
    strFilter = "(&(objectCateg ory=person)(obj ectClass=user)) "
    strQuery = strBase & ";" & strFilter & ";cn;subtre e"
    objCommand.Comm andText = strQuery
    objCommand.Prop erties("Page Size") = 100
    objCommand.Prop erties("Timeout ") = 30
    objCommand.Prop erties("Cache Results") = False
    Set objRecordSet = objCommand.Exec ute
    objRecordSet.Mo veFirst
    Do Until objRecordSet.EO F
    sName = objRecordSet.Fi elds("cn").Valu e
    'Response.Write (sName)
    genericAccount = "<option value= & '"sName"' & >"
    >
    objRecordSet.Mo veNext
    Loop
    %>
    <html><head><ti tle>Dynamic Drop-Down Menu Example</title></head>
    <body>
    <form method="POST" action="test3.a sp">
    <p><select size="1" name="GenericAc counts">
    <%=genericAccou nt%>
    </select></p>
    </form>
    </body>
    </html>
    >

    Comment

    • mcgrew.michael@gmail.com

      #3
      Re: Dynamic drop down list

      Thanks Michael. That did the trick.

      Michael Kujawa wrote:
      genericAccount = "<option value= & '"sName"' & >"
      >
      I would think should be
      >
      genericAccount = genericAccount & "<option value='" & sName & "'>" & sName &
      "</option>"
      >
      >
      >
      <mcgrew.michael @gmail.comwrote in message
      news:1156791520 .826402.266330@ 74g2000cwt.goog legroups.com...
      I hope this is the right group. I am very new to ASP so this is
      probably a stupid question. I have some vbscript that query's AD and
      populates a recordset. I know the recorset contains the information I
      want by doing a Response.write. I am having problems dynamically
      creating a drop down list from the data in the recordset. The drop down
      is created but it is empty. Any help would be greatly appreciated. A
      sample of the code:

      <%
      'On Error Resume Next
      Set objRootDSE = GetObject("LDAP ://RootDSE")
      strDNSDomain = objRootDSE.Get( "defaultNamingC ontext")
      strOU = "OU=Generic Accounts,"
      ' Use ADO to search Active Directory.
      Set objCommand = CreateObject("A DODB.Command")
      Set objConnection = CreateObject("A DODB.Connection ")
      objConnection.P rovider = "ADsDSOObje ct"
      objConnection.O pen "Active Directory Provider"
      objCommand.Acti veConnection = objConnection
      strBase = "<LDAP://" & strOU &" " & strDNSDomain &">"
      strFilter = "(&(objectCateg ory=person)(obj ectClass=user)) "
      strQuery = strBase & ";" & strFilter & ";cn;subtre e"
      objCommand.Comm andText = strQuery
      objCommand.Prop erties("Page Size") = 100
      objCommand.Prop erties("Timeout ") = 30
      objCommand.Prop erties("Cache Results") = False
      Set objRecordSet = objCommand.Exec ute
      objRecordSet.Mo veFirst
      Do Until objRecordSet.EO F
      sName = objRecordSet.Fi elds("cn").Valu e
      'Response.Write (sName)
      genericAccount = "<option value= & '"sName"' & >"

      objRecordSet.Mo veNext
      Loop
      %>
      <html><head><ti tle>Dynamic Drop-Down Menu Example</title></head>
      <body>
      <form method="POST" action="test3.a sp">
      <p><select size="1" name="GenericAc counts">
      <%=genericAccou nt%>
      </select></p>
      </form>
      </body>
      </html>

      Comment

      • Bob Barrows [MVP]

        #4
        Re: Dynamic drop down list

        mcgrew.michael@ gmail.com wrote:
        I hope this is the right group. I am very new to ASP so this is
        probably a stupid question. I have some vbscript that query's AD and
        populates a recordset. I know the recorset contains the information I
        want by doing a Response.write. I am having problems dynamically
        creating a drop down list from the data in the recordset. The drop
        down is created but it is empty. Any help would be greatly
        appreciated. A sample of the code:
        >
        <%
        'On Error Resume Next
        Set objRootDSE = GetObject("LDAP ://RootDSE")
        strDNSDomain = objRootDSE.Get( "defaultNamingC ontext")
        strOU = "OU=Generic Accounts,"
        ' Use ADO to search Active Directory.
        Set objCommand = CreateObject("A DODB.Command")
        Set objConnection = CreateObject("A DODB.Connection ")
        objConnection.P rovider = "ADsDSOObje ct"
        objConnection.O pen "Active Directory Provider"
        objCommand.Acti veConnection = objConnection
        strBase = "<LDAP://" & strOU &" " & strDNSDomain &">"
        strFilter = "(&(objectCateg ory=person)(obj ectClass=user)) "
        strQuery = strBase & ";" & strFilter & ";cn;subtre e"
        objCommand.Comm andText = strQuery
        objCommand.Prop erties("Page Size") = 100
        objCommand.Prop erties("Timeout ") = 30
        objCommand.Prop erties("Cache Results") = False
        Set objRecordSet = objCommand.Exec ute
        This line is not needed - upon opening, the recordset is already
        pointing at the first record unless EOF is true.:
        objRecordSet.Mo veFirst
        Do Until objRecordSet.EO F
        sName = objRecordSet.Fi elds("cn").Valu e
        'Response.Write (sName)
        Here is the problem:
        genericAccount = "<option value= & '"sName"' & >"
        Two problems:
        1. You are overwriting genericAccount with each pass through the loop.
        I' m sure that's not what you intended.
        2. You've put the quotes in the wrong places - this line should generate
        an error. I notice the first "On Error" line is commented out, so it's
        puzzling you aren't getting an error message. When you view the page's
        source in the browser, what do you see?
        The line should be:
        genericAccount = genericAccount & "<option value='" & sName & "'>"


        Anyways, this is an inefficient way to accomplish this task. Here is
        what I would do:

        Set objRecordSet = objCommand.Exec ute
        dim arData, i
        If Not objRecordSet.EO F then arData = objRecordSet.Ge tRows(,,"cn")
        objRecordSet.cl ose : set objRecordSet= nothing
        if isArray(arData) then
        for i = 0 to ubound(arData,2 )
        genericAccount = genericAccount & "<option value='" & _
        arData(0,i) & "'>"
        next
        end if



        --
        Microsoft MVP -- ASP/ASP.NET
        Please reply to the newsgroup. The email account listed in my From
        header is my spam trap, so I don't check it very often. You will get a
        quicker response by posting to the newsgroup.


        Comment

        • Adrienne Boswell

          #5
          Re: Dynamic drop down list


          mcgrew.michael@ gmail.com wote:
          I hope this is the right group. I am very new to ASP so this is
          probably a stupid question.
          There is no such thing as a stupid question, just people acting stupid.
          I have some vbscript that query's AD and
          populates a recordset. I know the recorset contains the information I
          want by doing a Response.write. I am having problems dynamically
          creating a drop down list from the data in the recordset. The drop down
          is created but it is empty. Any help would be greatly appreciated. A
          sample of the code:
          >
          <snip>
          objRecordSet.Mo veFirst
          Do Until objRecordSet.EO F
          sName = objRecordSet.Fi elds("cn").Valu e
          'Response.Write (sName)
          genericAccount = "<option value= & '"sName"' & >"
          >
          objRecordSet.Mo veNext
          Loop
          'Response.Write (sName)
          genericAccount = "<option value= & '"sName"' & >"
          >
          objRecordSet.Mo veNext
          Loop
          %>
          <html><head><ti tle>Dynamic Drop-Down Menu Example</title></head>
          <body>
          <form method="POST" action="test3.a sp">
          <p><select size="1" name="GenericAc counts">
          <%=genericAccou nt%>
          </select></p>
          </form>
          </body>
          </html>
          You have your loop in a strange place. You can create the value as a
          string, and then place it in your HTML, or you can generate it in the
          HTML. But, you should not be regenerating the same HTML over and over
          again. Eg:

          <% 'run query and open recordset %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
          "http://www.w3.org/TR/REC-html40/strict.dtd">
          <html lang="en">
          <head>
          <title>Dynami c Drop-Down Menu Example</title>
          </head>
          <body>
          <form method="POST" action="test3.a sp">
          <label for="genericacc ounts">Accounts </label>
          <select name="genericac counts" id="genericacco unts">
          <% while not objRecordset.EO F
          sName = objRecordSet.Fi elds("cn").Valu e
          %>
          <option value="<%=sName %>" <% if genericaccounts = sname
          then%>selected= "selected"< %end if%>><%=sName%> </option>
          <% objRecordset.Mo venext
          wend
          objRecordset.Cl ose
          set objRecordset = nothing
          'close connection to db if not needed again on page
          %>
          </select>
          </form>
          </body>
          </html>

          OR:

          <%
          while not objrecordset.EO F
          genericaccounts = genericaccounts "<option value=" & chr(034) & sname &
          chr(034) & "</option>&vbcrlf"
          objrecordset.Mo venext
          'close recordset and close connection if not needed again
          %>
          <select name="genericac counts">
          <%=genericaccou nts%>
          </select>
          </form>

          OR even better would be to use the GetString method described here:
          <http://www.4guysfromro lla.com/webtech/102600-1.shtml>

          Do you see? You were overwriting the HTML over and over again. It is
          also nice to show a value selected if the user has selected it. I
          would _strongly_ suggest closing elements, it makes it easier to debug.
          Although the closing tag for the option element is not required, it
          might make your life easier.

          --
          Adrienne Boswell at work
          Administrator nextBlock.com

          Please respond to the group so others can share

          Comment

          • Bob Barrows [MVP]

            #6
            Re: Dynamic drop down list

            Adrienne Boswell wrote:
            >
            You have your loop in a strange place. You can create the value as a
            string, and then place it in your HTML, or you can generate it in the
            HTML. But, you should not be regenerating the same HTML over and over
            again. Eg:
            I, on the other hand, think it's easier for maintenance if server-side
            code is separated from html as much as possible, so I firmly agree with
            his choice to run his loop before the <htmlelement, set his html to a
            variable and response.write the variable later on inside the <html>
            element. However, this is just a personal preference.


            --
            Microsoft MVP -- ASP/ASP.NET
            Please reply to the newsgroup. The email account listed in my From
            header is my spam trap, so I don't check it very often. You will get a
            quicker response by posting to the newsgroup.


            Comment

            • Dave Anderson

              #7
              Re: Dynamic drop down list

              Bob Barrows [MVP] wrote:
              Anyways, this is an inefficient way to accomplish this task.
              Here is what I would do:
              >
              Set objRecordSet = objCommand.Exec ute
              dim arData, i
              If Not objRecordSet.EO F then arData = objRecordSet.Ge tRows(,,"cn")
              objRecordSet.cl ose : set objRecordSet= nothing
              if isArray(arData) then
              for i = 0 to ubound(arData,2 )
              genericAccount = genericAccount & "<option value='" & _
              arData(0,i) & "'>"
              next
              end if
              And if your data has an apostrophe in it? I would opt for something more
              like this:

              genericAccount = genericAccount & "<option value=""" & _
              Server.HTMLEnco de(arData(0,i)) & """>" & _
              Server.HTMLEnco de(arData(0,i)) & "</option>"

              I always use double quotes and Server.HTMLEnco de() to write attributes
              values from data sources outside my control. To be more precise, I would
              also use HTMLEncode once per data item, would optionally set the selected
              attribute, and would do the whole thing in JScript.



              --
              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.


              Comment

              Working...