accessing value of dynamic select box from vbscript runat=server

Discussion in 'ASP General' started by skeddy@gmail.com, Jun 22, 2006.

  1. Guest

    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=VBSCRIPT RUNAT=SERVER>
    Public Sub BuildComboBox(rs, 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(dispname) & "</OPTION>")
    else
    response.write("<OPTION VALUE=" & rs.fields(val) & ">")
    response.write(rs.fields(dispname) & "</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 =
    DepartmentsSelectBox.Value"

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

    Mucho Thanks,
    sara
     
    , Jun 22, 2006
    #1
    1. Advertising

  2. Bob Lehmann Guest

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

    >> I'm sure my co-workers are tired of my whining.


    So, stop whining.

    Bob Lehmann

    <> wrote in message
    news:...
    > 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=VBSCRIPT RUNAT=SERVER>
    > Public Sub BuildComboBox(rs, 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(dispname) & "</OPTION>")
    > else
    > response.write("<OPTION VALUE=" & rs.fields(val) & ">")
    > response.write(rs.fields(dispname) & "</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 =
    > DepartmentsSelectBox.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, Jun 22, 2006
    #2
    1. Advertising

  3. wrote:
    > 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=VBSCRIPT RUNAT=SERVER>
    > Public Sub BuildComboBox(rs, 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(dispname) & "</OPTION>")
    > else
    > response.write("<OPTION VALUE=" & rs.fields(val) & ">")
    > response.write(rs.fields(dispname) & "</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 =
    > DepartmentsSelectBox.Value"


    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><form method="post">
    <%
    if request.form("cboBox")="" then
    BuildComboBox
    else
    Response.write request.form("cboBox")
    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"
     
    Bob Barrows [MVP], Jun 22, 2006
    #3
  4. Bob Guest

    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("name of select
    object").

    HTH
     
    Bob, Jun 22, 2006
    #4
  5. Guest

    Bob Barrows [MVP] wrote:

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


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

    <script LANGUAGE="VBScript" runat="server">
    Protected Sub showDropDownList(ByVal table)

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

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

    '-- 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=""selected""
    value="""">Choose....</option>")

    '-- loop and build each database entry as a selectable
    option
    While rs.EOF = False
    Response.Write("<option value=" & rs.Fields(0).Value &
    ">" _
    & rs.Fields(1).Value & "</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(ByVal sender As Object, ByVal e As
    System.EventArgs)

    ' 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.Value
    lg_desc = LgDescText.Value
    is_active = ActiveRadioButtonList.SelectedItem.Value

    ' Creating the Connection Object and opening the database
    con_str = "DRIVER={SQL Server};.....;"
    con = Server.CreateObject("ADODB.Connection")
    con.Open(con_str)

    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.Redirect("~/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="subWorkCombo" runat="server">
    <%
    Call showDropDownList("UnitOfMeasureTypes")
    %>
    </select>

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

    Thank you all so much for the quick replies!
    sara
     
    , Jun 22, 2006
    #5
  6. Guest

    Bob Barrows [MVP] wrote:

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


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

    <script LANGUAGE="VBScript" runat="server">
    Protected Sub showDropDownList(ByVal table)

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

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

    '-- 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=""selected""
    value="""">Choose....</option>")

    '-- loop and build each database entry as a selectable
    option
    While rs.EOF = False
    Response.Write("<option value=" & rs.Fields(0).Value &
    ">" _
    & rs.Fields(1).Value & "</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(ByVal sender As Object, ByVal e As
    System.EventArgs)

    ' 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.Value
    lg_desc = LgDescText.Value
    is_active = ActiveRadioButtonList.SelectedItem.Value

    ' Creating the Connection Object and opening the database
    con_str = "DRIVER={SQL Server};.....;"
    con = Server.CreateObject("ADODB.Connection")
    con.Open(con_str)

    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.Redirect("~/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="subWorkCombo" runat="server">
    <%
    Call showDropDownList("UnitOfMeasureTypes")
    %>
    </select>

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

    Thank you all so much for the quick replies!
    sara
     
    , Jun 22, 2006
    #6
  7. wrote:
    > Bob Barrows [MVP] wrote:
    >
    >> 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.

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


    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

    > Public Sub SaveBtn_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs)


    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.public.dotnet.framework.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.
     
    Bob Barrows [MVP], Jun 22, 2006
    #7
  8. Guest

    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:
    > wrote:
    > > Bob Barrows [MVP] wrote:
    > >
    > >> 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.

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

    >
    > 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
    >
    > > Public Sub SaveBtn_Click(ByVal sender As Object, ByVal e As
    > > System.EventArgs)

    >
    > 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.public.dotnet.framework.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.
     
    , Jun 22, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    Client-Side JavaScript Problem in runat=server "<SELECT>"

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 26, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    3,700
    Steven Cheng[MSFT]
    May 1, 2006
  2. Replies:
    0
    Views:
    2,100
  3. baldrick
    Replies:
    10
    Views:
    2,436
    nick chan
    Jan 22, 2008
  4. Robin
    Replies:
    2
    Views:
    198
    Robin
    Apr 9, 2004
  5. palmiere
    Replies:
    1
    Views:
    473
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page