Dynamically selecting a value in a listbox from a passed variable

L

Lukelrc

Hi,

I have a dynamically created listbox. I'm trying to get one of the
options selected according to a passed value. This is what i have:

<select name="txtTheme" id="txtTheme">

<option>Select Theme</option>
<%
Dim PassedTheme
Dim PickThis
PassedTheme = Request("txtTheme") %>
<% While (NOT RsThemes.EOF)%>

<% If Not rsChosenEvent.EOF Or Not rsChosenEvent.BOF Then %><% If
PassedTheme = (RsChosenEvent.Fields.Item("Theme").Value) Then Pickthis
= " SELECTED " %><% End IF %>

<option <%= PickThis %>
value="<%=(RsThemes.Fields.Item("ThemeID").Value)%>"><%=(RsThemes.Fields.Item("ThemeName").Value)%></option>
<%
RsThemes.MoveNext()
Wend
If (RsThemes.CursorType > 0) Then
RsThemes.MoveFirst
Else
RsThemes.Requery
End If
%>

but for some reason PickThis is never writes " SELECTED " in the HTML
when it sould (i.e when Request("txtTheme") =
(RsChosenEvent.Fields.Item("Theme").Value)). I can't see why this
shouldn't work. Does anyone know how i can do this/workaround this?
 
R

Ray at

Have you response.Written Request("txtTheme") to verify the value?

Is it a text value or numeric? It seems you are not using the ThemeID field
to pass the theme. Why?

Instead of Request("txtTheme"), please use Request.Querystring("txtTheme")
(or Request.Form, Request.Cookies...... Point being, specify what you mean.
(http://www.aspfaq.com/show.asp?id=2111)

And may I suggest a bit different code:

<select name="txtTheme" id="txtTheme">

<option>Select Theme</option>
<%
Dim PassedTheme
Dim PickThis
Dim bMatch, sValue, sDisplay

PassedTheme = Request.QUERYSTRING("txtTheme")

While Not RsThemes.EOF
sValue = RsThemes.Fields.Item("ThemeID").Value
sDisplay = RsThemes.Fields.Item("ThemeName").Value
bMatch = RsThemes.Fields.Item("Theme").Value = PassedTheme
%>
<option value="<%=sValue%>"<% If bMatch Then Response.Write "
selected"%>><%=sDisplay%></option>
<%
RsThemes.MoveNext()
Wend
%>



Ray at home
 
L

Luke Curtis

Hi,

I had tried to simplify my problem for the ease of explanation but I’ll
try to explain it now:

I have a site in which users can add an update to a list of events in
their area. When the user wishes to update an event they select an event
from a list and are taken to an ‘update page’ populated by a recordset
named rsChosenEvent. One of the fields in rsChosenEvents is themes. So
on the update page I have a list box populated by rsThemes which has
values set as rsThemes.ThemeID.value and the label as
rsThemes.ThemeName.value. So initially the theme is selected by:

<% If Not rsChosenEvent.EOF Or Not rsChosenEvent.BOF Then %><% If
(RsThemes.Fields.Item("ThemeID").Value) =
(RsChosenEvent.Fields.Item("Theme").Value) Then
Response.write("SELECTED ") %><% End IF %>

This is all fine and all works. What complicates matters is that I need
a button next to the themes listbox which takes users to a ‘theme adding
page’ in which they can add new themes. When they browse back to the
update page again I need all of the values that they had already updates
to be intact. So what I have done is added a group of hidden fields to
the theme adding page which carry the fields values back to the update
page again. At this point the rsChosenEvents is empty and the update
page is now populate dby the values passed from the hidden fields. So
now I need to slect a theme by something along the lines of:

<% if Request(“txtTheme”) = (RsThemes.Fields.Item("ThemeID").Value) then
response.write(“ SELECTED “) %> but if I put this inside the option tags
it never works.

This is the code I have at the moment which I’ve tried playing with:

<select name="txtTheme" id="txtTheme" style="width:120px;">
<option>Select Theme</option>
<%
Dim PassedTheme
Dim PickThis
PassedTheme = Request("txtTheme") %>
<% While (NOT RsThemes.EOF)%>
<% If Not rsChosenEvent.EOF Or Not rsChosenEvent.BOF Then %><% If
PassedTheme = (RsChosenEvent.Fields.Item("Theme").Value) Then Pickthis =
" SELECTED " %><% End IF %>

<option <% If Not rsChosenEvent.EOF Or Not rsChosenEvent.BOF Then %><%
If (RsThemes.Fields.Item("ThemeID").Value) =
(RsChosenEvent.Fields.Item("Theme").Value) Then
Response.write("SELECTED ") %><% End IF %><%= PickThis %><%IF
(RsThemes.Fields.Item("ThemeID").Value) = PassedTheme Then Choosethis =
" SELECTED " End IF %> <%= ChooseThis %>
value="<%=(RsThemes.Fields.Item("ThemeID").Value)%>"><%=(RsThemes.Fields
..Item("ThemeName").Value)%></option>
<%
RsThemes.MoveNext()
Wend
If (RsThemes.CursorType > 0) Then
RsThemes.MoveFirst
Else
RsThemes.Requery
End If
%>
</select>

I hope my explanation is clear enough. Do you have any idea why this
doesn’t work?

Thanks, Luke


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
R

Ray at

Have you response.Written Request("txtTheme") to verify the value?

Is it a text value or numeric? It seems you are not using the ThemeID field
to pass the theme. Why?

Ray at work
 
L

Luke Curtis

Like i said,

Its Numeric, and if i response.write("txtTheme") i get the correct
value.

It is the ThemeID that is passed. I.e txtTheme's value is ThemeID and
its Label is ThemeName.

Luke
 
R

Ray at

But what you're using the check for a match is the theme name, not the ID.
I suggest that you pass the theme ID in the querystring and try this code:

sample url:
/yourpage.asp?themeid=3

<select name="txtTheme" id="txtTheme">

<option>Select Theme</option>
<%
Dim iPassedTheme
Dim PickThis
Dim bMatch, iThemeID, sDisplay

iPassedTheme = "0" & Request.QUERYSTRING("themeID")
If Not IsNumeric(iPassedTheme) Then iPassedTheme = 0

While Not RsThemes.EOF
iTheme = RsThemes.Fields.Item("ThemeID").Value
sDisplay = RsThemes.Fields.Item("ThemeName").Value
bMatch = CInt(iThemeID) = CInt(iPassedTheme)
%>
<option value="<%=sValue%>"<% If bMatch Then Response.Write "
selected"%>><%=sDisplay%></option>
<%
RsThemes.MoveNext()
Wend
%>

Ray at work
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top