For Programmers: Free Programming Magazines  


Home > Archive > ASP > January 2007 > Select (dropdown) list and set value based on table column









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Select (dropdown) list and set value based on table column
mcauliffe

2007-01-23, 6:57 pm

I have an old application ( pre-VB5) that I need to add a select/option list
to. This is an edit program so the values for the form will be retrieved
from a database. How do I set the value of the dropdown with the value from
the database. The value in the database is either new, trial, maint.,
employee, beta, or null. I need to set the dropdrown to one of these values.

An example of the select;
<td>
<select name="ordReason">
<option></option>
<option value="New">New</option>
<option value="Maint.">Maint.</option>
<option value="Trial">Trial</option>
<option value="Employee">Employee</option>
<option value="Beta">Beta</option>
</select>
</td>
Bob Barrows [MVP]

2007-01-23, 6:57 pm

mcauliffe wrote:
> I have an old application ( pre-VB5) that I need to add a
> select/option list to. This is an edit program so the values for the
> form will be retrieved from a database. How do I set the value of
> the dropdown with the value from the database. The value in the
> database is either new, trial, maint., employee, beta, or null. I
> need to set the dropdrown to one of these values.
>
> An example of the select;
> <td>
> <select name="ordReason">
> <option></option>
> <option value="New">New</option>
> <option value="Maint.">Maint.</option>
> <option value="Trial">Trial</option>
> <option value="Employee">Employee</option>
> <option value="Beta">Beta</option>
> </select>
> </td>


When adding the options, add the word " checked" to the option tag of
the one you want selected.

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


mcauliffe

2007-01-23, 6:57 pm

Thank you for the reply.

I don't know which one will bew selected until I retrieved a record from the
database. The user is editing a existing record and may changed the value of
the dropdown. I must first indicate the value is the database.

An example: the database for the column is equal to "Employee" How do I
indicate when I display the ASP page on the form that the existing value of
ordReason is Employee?

"Bob Barrows [MVP]" wrote:

> mcauliffe wrote:
>
> When adding the options, add the word " checked" to the option tag of
> the one you want selected.
>
> --
> 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]

2007-01-23, 6:57 pm

mcauliffe wrote:
> Thank you for the reply.
>
> I don't know which one will bew selected until I retrieved a record
> from the database. The user is editing a existing record and may
> changed the value of the dropdown. I must first indicate the value
> is the database.
>
> An example: the database for the column is equal to "Employee" How
> do I indicate when I display the ASP page on the form that the
> existing value of ordReason is Employee?
>


Concatenate "checked" into the option value text when building the
option string.

You are building these options by looping through some records in a
recordset, correct? When you get to the one that contains "Employee",
concatenate "selected" (not "checked" - oops) into the option tag.
Obviously, this means you need to know/retrieve the selected value
before building the option list.

Here's a simple example using an array instead of a recordset (since i
don't have access to your database, of course):

<%
dim options, ar, selectedvalue, val
selectedvalue=Request.Form("sel")
ar=array("New","Maint.","Trial","Employee","Beta")
for each val in ar
options=options & "<option value=""" & val & """"
if val=selectedvalue then options=options & " selected"
options=options & ">" & val
next
%>
<html><body><form method="post">
<select name="sel">
<%=options%>
</select><br>
<input type="submit">
</form></body></html>

next


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


Michael

2007-01-24, 7:56 am

I can recommend my procedure for building select box.
parameter "arr" is 2 dimensional array taken from query and based on 2
fields; id and name, using method getrows
parameter "id" is value that should be checked

Sub FillSelectBox(arr, selectname, action, size, id)
Dim Sel
Response.Write "<select name=" & selectname & " onchange=" & action
& " style='width:" & size & "px;'>"
For i=0 to Ubound(arr,2)
If cint(id)=cint(arr(0,i)) Then
Sel=" selected "
Else
Sel=" "
End If
Response.Write "<option" & Sel & " value=" & arr(0,i) & ">"
& arr(1,i) & "</option>"
Next
Response.write "</select>"
End Sub


example of calling
<%
sql="Select CustomerId, CustomerName from Customers order by CustomerName"
call getfromdatabase(sql, rs) ' your own function to get query
If Not rs.eof Then
array=rs.GetRows
End If
call FillSelectBox(array, "Types", "changesomething(" & rs("CustomerId") &
")", 20, "Smith")
%>


Michael


mcauliffe

2007-01-24, 6:56 pm

Thank you for the answer.

As a newbie, needed something that I would undetstand. Your response help
me. I did a Select Case and resolved.

This is a snipet of the code
Select Case rsorder_header.fields.getValue("order_reason")
Case ""
Response.Write "<td width=100>" & _
"<select name='ordReason'>" & _
"<option Selected></option>" & _
"<option value='New'>New</option>" & _
"<option value='Maint.'>Maint.</option>" & _
"<option value='Trial'>Trial</option>" & _
"<option Value='Employee'>Employee</option>" & _
"<option value='Beta'>Beta</option></select></td>"
Case "New"
Response.Write "<td width=100>" & _
"<select name='ordReason'>" & _
"<option></option>" & _
"<option value='New' Selected>New</option>" & _
"<option value='Maint.'>Maint.</option>" & _
"<option value='Trial'>Trial</option>" & _
"<option Value='Employee'>Employee</option>" & _
"<option value='Beta'>Beta</option></select></td>"
Case "Maint."
Response.Write "<td width=100>" & _
"<select name='ordReason'>" & _
"<option></option>" & _
"<option value='New'>New</option>" & _
"<option value='Maint.' Selected>Maint.</option>" & _
"<option value='Trial'>Trial</option>" & _
"<option Value='Employee'>Employee</option>" & _
"<option value='Beta'>Beta</option></select></td>"

"Michael" wrote:

> I can recommend my procedure for building select box.
> parameter "arr" is 2 dimensional array taken from query and based on 2
> fields; id and name, using method getrows
> parameter "id" is value that should be checked
>
> Sub FillSelectBox(arr, selectname, action, size, id)
> Dim Sel
> Response.Write "<select name=" & selectname & " onchange=" & action
> & " style='width:" & size & "px;'>"
> For i=0 to Ubound(arr,2)
> If cint(id)=cint(arr(0,i)) Then
> Sel=" selected "
> Else
> Sel=" "
> End If
> Response.Write "<option" & Sel & " value=" & arr(0,i) & ">"
> & arr(1,i) & "</option>"
> Next
> Response.write "</select>"
> End Sub
>
>
> example of calling
> <%
> sql="Select CustomerId, CustomerName from Customers order by CustomerName"
> call getfromdatabase(sql, rs) ' your own function to get query
> If Not rs.eof Then
> array=rs.GetRows
> End If
> call FillSelectBox(array, "Types", "changesomething(" & rs("CustomerId") &
> ")", 20, "Smith")
> %>
>
>
> Michael
>
>
>

Evertjan.

2007-01-24, 6:56 pm

=?Utf-8?B?bWNhdWxpZmZl?= wrote on 24 jan 2007 in
microsoft.public.inetserver.asp.general:

> As a newbie, needed something that I would undetstand. Your response
> help me. I did a Select Case and resolved.
>
> This is a snipet of the code
> Select Case rsorder_header.fields.getValue("order_reason")
> Case ""
> Response.Write "<td width=100>" & _
> "<select name='ordReason'>" & _
> "<option Selected></option>" & _
> "<option value='New'>New</option>" & _
> "<option value='Maint.'>Maint.</option>" & _
> "<option value='Trial'>Trial</option>" & _
> "<option Value='Employee'>Employee</option>" & _
> "<option value='Beta'>Beta</option></select></td>"
> Case "New"
> Response.Write "<td width=100>" & _
> "<select name='ordReason'>" & _
> "<option></option>" & _
> "<option value='New' Selected>New</option>" & _
> "<option value='Maint.'>Maint.</option>" & _
> "<option value='Trial'>Trial</option>" & _
> "<option Value='Employee'>Employee</option>" & _
> "<option value='Beta'>Beta</option></select></td>"
> Case "Maint."
> Response.Write "<td width=100>" & _
> "<select name='ordReason'>" & _
> "<option></option>" & _
> "<option value='New'>New</option>" & _
> "<option value='Maint.' Selected>Maint.</option>"
> & _ "<option value='Trial'>Trial</option>" & _
> "<option Value='Employee'>Employee</option>" & _
> "<option value='Beta'>Beta</option></select></td>"
>


Why not use ASP-VBS to optimize your code:


<%
Function writeOption(t)
If rsn = t Then s = "Selected" Else s = ""
Response.Write "<option value='"&t&"'"&s&">"&t&"</option>" & VbCrLf
End Function

rsn = rsorder_header.fields.getValue("order_reason")
%>
<td width=100>
<select name='ordReason'>
<%
writeOption("")
writeOption("New")
writeOption("Maint.")
writeOption("Trial")
writeOption("Employee")
writeOption("Beta")
%>
</select>
</td>

Not tested btw.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Billy

2007-01-26, 6:56 pm

Thanks all. Commenting out the section that produced the headers
worked.

On Jan 24, 10:22 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
> =?Utf-8?B?bWNhdWxpZmZl?= wrote on 24 jan 2007 in
> microsoft.public.inetserver.asp.general:
>
>
>
>
>
>
>
> <%
> Function writeOption(t)
> If rsn = t Then s = "Selected" Else s = ""
> Response.Write "<option value='"&t&"'"&s&">"&t&"</option>" & VbCrLf
> End Function
>
> rsn = rsorder_header.fields.getValue("order_reason")
> %>
> <td width=100>
> <select name='ordReason'>
> <%
> writeOption("")
> writeOption("New")
> writeOption("Maint.")
> writeOption("Trial")
> writeOption("Employee")
> writeOption("Beta")
> %>
> </select>
> </td>
>
> Not tested btw.
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)- Hide quoted text -- Show quoted text -


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com