accessing value of dynamic select box from vbscript runat=server

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

    accessing value of dynamic select box from vbscript runat=server

    In a nutshell, I'm trying to dynamically create a select box with
    ResultSet code in vbscript and then need to be able to access the value
    of that select box later with a Save button.

    I've got the select box filling with code similar to below:

    <SCRIPT LANGUAGE=VBSCRI PT RUNAT=SERVER>
    Public Sub BuildComboBox(r s, dispname, val, name, selected)
    'rs = the recordset
    'val = fieldname to place in the val of the option
    'dispname = fieldname to display in the user control
    'name = name of the select (cboBox)
    'selected is the item in the lst that should be selected

    rs.movefirst

    response.write( "<SELECT NAME=" & name & ">")

    while not rs.eof
    if rs.fields(val) = selected then
    response.write( "<OPTION VALUE=" & rs.fields(val) & " selected>")
    response.write( rs.fields(dispn ame) & "</OPTION>")
    else
    response.write( "<OPTION VALUE=" & rs.fields(val) & ">")
    response.write( rs.fields(dispn ame) & "</OPTION>")
    end if
    rs.MoveNext
    wend

    response.write( "</SELECT>")
    End Sub

    </SCRIPT>

    Peachy. Now I need to get to the user-selected value of that box when
    they hit the Save button. My issue with vbscript thus far is that
    it'll create me a select box dynamically in the runat=server code (as
    above). But another function in that same vbscript section can't find
    my select box. It seems to only recognize it if I statically create it
    in html, not in vbscript. Oh, and I'm using something like this in the
    vbscript to get the select box's value: "dept =
    DepartmentsSele ctBox.Value"

    Help? I've been cursing vbscript all day, and I'm sure my co-workers
    are tired of my whining.

    Mucho Thanks,
    sara

  • Bob Lehmann

    #2
    Re: accessing value of dynamic select box from vbscript runat=server

    >> But another function in that same vbscript section can't find my select
    box

    Then, why didn't you show that code if that's where the problem is?
    [color=blue][color=green]
    >> I'm sure my co-workers are tired of my whining.[/color][/color]

    So, stop whining.

    Bob Lehmann

    <skeddy@gmail.c om> wrote in message
    news:1150933076 .684538.313290@ y41g2000cwy.goo glegroups.com.. .[color=blue]
    > In a nutshell, I'm trying to dynamically create a select box with
    > ResultSet code in vbscript and then need to be able to access the value
    > of that select box later with a Save button.
    >
    > I've got the select box filling with code similar to below:
    >
    > <SCRIPT LANGUAGE=VBSCRI PT RUNAT=SERVER>
    > Public Sub BuildComboBox(r s, dispname, val, name, selected)
    > 'rs = the recordset
    > 'val = fieldname to place in the val of the option
    > 'dispname = fieldname to display in the user control
    > 'name = name of the select (cboBox)
    > 'selected is the item in the lst that should be selected
    >
    > rs.movefirst
    >
    > response.write( "<SELECT NAME=" & name & ">")
    >
    > while not rs.eof
    > if rs.fields(val) = selected then
    > response.write( "<OPTION VALUE=" & rs.fields(val) & " selected>")
    > response.write( rs.fields(dispn ame) & "</OPTION>")
    > else
    > response.write( "<OPTION VALUE=" & rs.fields(val) & ">")
    > response.write( rs.fields(dispn ame) & "</OPTION>")
    > end if
    > rs.MoveNext
    > wend
    >
    > response.write( "</SELECT>")
    > End Sub
    >
    > </SCRIPT>
    >
    > Peachy. Now I need to get to the user-selected value of that box when
    > they hit the Save button. My issue with vbscript thus far is that
    > it'll create me a select box dynamically in the runat=server code (as
    > above). But another function in that same vbscript section can't find
    > my select box. It seems to only recognize it if I statically create it
    > in html, not in vbscript. Oh, and I'm using something like this in the
    > vbscript to get the select box's value: "dept =
    > DepartmentsSele ctBox.Value"
    >
    > Help? I've been cursing vbscript all day, and I'm sure my co-workers
    > are tired of my whining.
    >
    > Mucho Thanks,
    > sara
    >[/color]


    Comment

    • Bob Barrows [MVP]

      #3
      Re: accessing value of dynamic select box from vbscript runat=server

      skeddy@gmail.co m wrote:[color=blue]
      > In a nutshell, I'm trying to dynamically create a select box with
      > ResultSet code in vbscript and then need to be able to access the
      > value of that select box later with a Save button.
      >
      > I've got the select box filling with code similar to below:
      >
      > <SCRIPT LANGUAGE=VBSCRI PT RUNAT=SERVER>
      > Public Sub BuildComboBox(r s, dispname, val, name, selected)
      > 'rs = the recordset
      > 'val = fieldname to place in the val of the option
      > 'dispname = fieldname to display in the user control
      > 'name = name of the select (cboBox)
      > 'selected is the item in the lst that should be selected
      >
      > rs.movefirst
      >
      > response.write( "<SELECT NAME=" & name & ">")
      >
      > while not rs.eof
      > if rs.fields(val) = selected then
      > response.write( "<OPTION VALUE=" & rs.fields(val) & " selected>")
      > response.write( rs.fields(dispn ame) & "</OPTION>")
      > else
      > response.write( "<OPTION VALUE=" & rs.fields(val) & ">")
      > response.write( rs.fields(dispn ame) & "</OPTION>")
      > end if
      > rs.MoveNext
      > wend
      >
      > response.write( "</SELECT>")
      > End Sub
      >
      > </SCRIPT>
      >
      > Peachy. Now I need to get to the user-selected value of that box when
      > they hit the Save button. My issue with vbscript thus far is that
      > it'll create me a select box dynamically in the runat=server code (as
      > above). But another function in that same vbscript section can't find
      > my select box. It seems to only recognize it if I statically create
      > it in html, not in vbscript. Oh, and I'm using something like this
      > in the vbscript to get the select box's value: "dept =
      > DepartmentsSele ctBox.Value"[/color]

      What have you tried (show us what you really have instead of "something
      like" what you have).

      It never hurts to give us a small repro page to play with. By "repro page" I
      mean a page where nothing except the bits related to your problem exist.
      Something like:

      <html><body><fo rm method="post">
      <%
      if request.form("c boBox")="" then
      BuildComboBox
      else
      Response.write request.form("c boBox")
      end if
      Sub BuildComboBox()
      response.write "<SELECT NAME=""cboBox"" >"
      dim i
      for i = 68 to 75
      response.write "<option value=""" & i & """>" & chr(i) & vbcrlf
      next
      response.write "</select>"
      End Sub
      %>
      <br><input type="submit" value="Save">
      </form></body></html>


      --
      Microsoft MVP - ASP/ASP.NET
      Please reply to the newsgroup. This email account is my spam trap so I
      don't check it very often. If you must reply off-line, then remove the
      "NO SPAM"


      Comment

      • Bob

        #4
        Re: accessing value of dynamic select box from vbscript runat=server

        Let me get this straight. In server side code you are creating a drop
        down select box to be sent to the client. In the same server side code
        you are trying to access the value of that select box before it is sent
        to the client?

        If you want the value selected you'll have to wait until the client
        submits the form. Then in the page defined in the action parameter of
        the form you can get the value using Request.Form("n ame of select
        object").

        HTH

        Comment

        • skeddy@gmail.com

          #5
          Re: accessing value of dynamic select box from vbscript runat=server


          Bob Barrows [MVP] wrote:
          [color=blue]
          > What have you tried (show us what you really have instead of "something
          > like" what you have).
          >
          > It never hurts to give us a small repro page to play with. By "repro page" I
          > mean a page where nothing except the bits related to your problem exist.[/color]

          Here's what I have, paired down for the sake of clarity:

          <script LANGUAGE="VBScr ipt" runat="server">
          Protected Sub showDropDownLis t(ByVal table)

          '-- SQL Statement
          strSQL = "SELECT * FROM " & table & " ORDER BY 1"

          '-- Execute our SQL statement and store the recordset
          rs = con.Execute(str SQL)

          '-- If we have records to return
          If Not rs.EOF Then
          '-- Open a form/select tag THE NAME OF THE SELECT IS
          DEFINED HERE
          Response.Write( "<select name=" & table & "Select""
          style=""width: 155px"" runat=""server" ">")
          Response.Write( "<option selected=""sele cted""
          value="""">Choo se....</option>")

          '-- loop and build each database entry as a selectable
          option
          While rs.EOF = False
          Response.Write( "<option value=" & rs.Fields(0).Va lue &
          ">" _
          & rs.Fields(1).Va lue & "</option>")

          '-- Move recordset to the next value
          rs.movenext()
          End While
          End If
          '--END OF MAIN CODE BLOCK

          '-- close select/form tags
          Response.Write( "</select>")
          End Sub

          Public Sub SaveBtn_Click(B yVal sender As Object, ByVal e As
          System.EventArg s)

          ' Setting variables
          Dim sql_insert, sql_numrows, num_rows, rs
          Dim account, upc, sku, plu, dept, ...etc
          num_rows = 0

          ' Receiving values from Form (abbreviated list here..)
          dept = AccountsSelect. Value <------ BECAUSE THE NAME
          "AccountsSelect " IS CREATED IN THE CODE BLOCK ABOVE, I GET ERRORS HERE

          sh_desc = ShDescText.Valu e
          lg_desc = LgDescText.Valu e
          is_active = ActiveRadioButt onList.Selected Item.Value

          ' Creating the Connection Object and opening the database
          con_str = "DRIVER={SQ L Server};.....;"
          con = Server.CreateOb ject("ADODB.Con nection")
          con.Open(con_st r)

          sql_insert = "some string i cut out to make this code block
          shorter"
          ' Executing the sql insertion code
          con.Execute(sql _insert)

          ' Done. Now Close the connection
          con.Close()
          con = Nothing

          ' load the list page
          Response.Redire ct("~/item_list.aspx" )
          End Sub
          </script>

          Also, for more information, if I create the select in HTML (as opposed
          to creating it in the vbscript above), and then try to create the
          'option' lines in the vbscript, the 'runat=server' line won't let me
          type a select statement followed by script. e.g.,
          <select size="1" id="subwork" name="subWorkCo mbo" runat="server">
          <%
          Call showDropDownLis t("UnitOfMeasur eTypes")
          %>
          </select>

          The code inside the script tags is never hit as long as I have the
          "runat=serv er" prop in the select code. If I remove the
          "runat=serv er", the select box name can't be accessed from the SaveBtn
          sub.

          Thank you all so much for the quick replies!
          sara

          Comment

          • skeddy@gmail.com

            #6
            Re: accessing value of dynamic select box from vbscript runat=server


            Bob Barrows [MVP] wrote:
            [color=blue]
            > What have you tried (show us what you really have instead of "something
            > like" what you have).
            >
            > It never hurts to give us a small repro page to play with. By "repro page" I
            > mean a page where nothing except the bits related to your problem exist.[/color]

            Here's what I have, paired down for the sake of clarity:

            <script LANGUAGE="VBScr ipt" runat="server">
            Protected Sub showDropDownLis t(ByVal table)

            '-- SQL Statement
            strSQL = "SELECT * FROM " & table & " ORDER BY 1"

            '-- Execute our SQL statement and store the recordset
            rs = con.Execute(str SQL)

            '-- If we have records to return
            If Not rs.EOF Then
            '-- Open a form/select tag THE NAME OF THE SELECT IS
            DEFINED HERE
            Response.Write( "<select name=" & table & "Select""
            style=""width: 155px"" runat=""server" ">")
            Response.Write( "<option selected=""sele cted""
            value="""">Choo se....</option>")

            '-- loop and build each database entry as a selectable
            option
            While rs.EOF = False
            Response.Write( "<option value=" & rs.Fields(0).Va lue &
            ">" _
            & rs.Fields(1).Va lue & "</option>")

            '-- Move recordset to the next value
            rs.movenext()
            End While
            End If
            '--END OF MAIN CODE BLOCK

            '-- close select/form tags
            Response.Write( "</select>")
            End Sub

            Public Sub SaveBtn_Click(B yVal sender As Object, ByVal e As
            System.EventArg s)

            ' Setting variables
            Dim sql_insert, sql_numrows, num_rows, rs
            Dim account, upc, sku, plu, dept, ...etc
            num_rows = 0

            ' Receiving values from Form (abbreviated list here..)
            dept = AccountsSelect. Value <------ BECAUSE THE NAME
            "AccountsSelect " IS CREATED IN THE CODE BLOCK ABOVE, I GET ERRORS HERE

            sh_desc = ShDescText.Valu e
            lg_desc = LgDescText.Valu e
            is_active = ActiveRadioButt onList.Selected Item.Value

            ' Creating the Connection Object and opening the database
            con_str = "DRIVER={SQ L Server};.....;"
            con = Server.CreateOb ject("ADODB.Con nection")
            con.Open(con_st r)

            sql_insert = "some string i cut out to make this code block
            shorter"
            ' Executing the sql insertion code
            con.Execute(sql _insert)

            ' Done. Now Close the connection
            con.Close()
            con = Nothing

            ' load the list page
            Response.Redire ct("~/item_list.aspx" )
            End Sub
            </script>

            Also, for more information, if I create the select in HTML (as opposed
            to creating it in the vbscript above), and then try to create the
            'option' lines in the vbscript, the 'runat=server' line won't let me
            type a select statement followed by script. e.g.,
            <select size="1" id="subwork" name="subWorkCo mbo" runat="server">
            <%
            Call showDropDownLis t("UnitOfMeasur eTypes")
            %>
            </select>

            The code inside the script tags is never hit as long as I have the
            "runat=serv er" prop in the select code. If I remove the
            "runat=serv er", the select box name can't be accessed from the SaveBtn
            sub.

            Thank you all so much for the quick replies!
            sara

            Comment

            • Bob Barrows [MVP]

              #7
              Re: accessing value of dynamic select box from vbscript runat=server

              skeddy@gmail.co m wrote:[color=blue]
              > Bob Barrows [MVP] wrote:
              >[color=green]
              >> What have you tried (show us what you really have instead of
              >> "something like" what you have).
              >>
              >> It never hurts to give us a small repro page to play with. By "repro
              >> page" I mean a page where nothing except the bits related to your
              >> problem exist.[/color]
              >
              > Here's what I have, paired down for the sake of clarity:[/color]

              You are definitely confused between classic ASP and ASP.Net. These are
              two totally different technologies.
              You are mixing vbscript (all variables are variants, adodb objects) with
              vb.net (all variables have datatypes, recordsets don't exist, etc.
              etc.) - not a good idea.

              Go back to the books and decide which technology you are going to use.

              If you decide on classic asp with vbscript, then things like
              [color=blue]
              > Public Sub SaveBtn_Click(B yVal sender As Object, ByVal e As
              > System.EventArg s)[/color]

              are not supported. For one thing, you see those "As ..." expressions?
              Those will cause an error in vbscript. For another, there are no
              server-side events in classic ASP: the only information about a request
              that is available to server-side code is contained in the Request
              collections.
              BTW, if you decide to go with classic ASP/vbscript, then go back and
              look at the repro page I posted ... it works. Try it and see.

              If you need to use .Net server-side events, then you need to use asp.net
              and either vb.net or C# (or whatever other managed language suits your
              fancy). For asp.net support go to
              microsoft.publi c.dotnet.framew ork.aspnet

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

              • skeddy@gmail.com

                #8
                Re: accessing value of dynamic select box from vbscript runat=server

                Thanks so much for the clarification. I'm a java (language, script,
                servlet) person, and I let VisualStudio give me what it could for
                "free" when making these asp pages. So, yeah, I'm new to this flavor
                of pages. I'll come back to this with a fresh mind and better
                direction.

                Thanks Much,
                sara


                Bob Barrows [MVP] wrote:[color=blue]
                > skeddy@gmail.co m wrote:[color=green]
                > > Bob Barrows [MVP] wrote:
                > >[color=darkred]
                > >> What have you tried (show us what you really have instead of
                > >> "something like" what you have).
                > >>
                > >> It never hurts to give us a small repro page to play with. By "repro
                > >> page" I mean a page where nothing except the bits related to your
                > >> problem exist.[/color]
                > >
                > > Here's what I have, paired down for the sake of clarity:[/color]
                >
                > You are definitely confused between classic ASP and ASP.Net. These are
                > two totally different technologies.
                > You are mixing vbscript (all variables are variants, adodb objects) with
                > vb.net (all variables have datatypes, recordsets don't exist, etc.
                > etc.) - not a good idea.
                >
                > Go back to the books and decide which technology you are going to use.
                >
                > If you decide on classic asp with vbscript, then things like
                >[color=green]
                > > Public Sub SaveBtn_Click(B yVal sender As Object, ByVal e As
                > > System.EventArg s)[/color]
                >
                > are not supported. For one thing, you see those "As ..." expressions?
                > Those will cause an error in vbscript. For another, there are no
                > server-side events in classic ASP: the only information about a request
                > that is available to server-side code is contained in the Request
                > collections.
                > BTW, if you decide to go with classic ASP/vbscript, then go back and
                > look at the repro page I posted ... it works. Try it and see.
                >
                > If you need to use .Net server-side events, then you need to use asp.net
                > and either vb.net or C# (or whatever other managed language suits your
                > fancy). For asp.net support go to
                > microsoft.publi c.dotnet.framew ork.aspnet
                >
                > --
                > 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.[/color]

                Comment

                Working...