accessing value of dynamic select box from vbscript runat=server

S

skeddy

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
 
B

Bob Lehmann

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?

So, stop whining.

Bob Lehmann
 
B

Bob Barrows [MVP]

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

Bob

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
 
S

skeddy

Bob said:
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
 
S

skeddy

Bob said:
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
 
B

Bob Barrows [MVP]

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
 
S

skeddy

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top