By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 2,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Dynamically selecting a value in a listbox from a passed variable

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"Lukelrc" <lu*********@westoxon.gov.uk> wrote in message
news:20*************************@posting.google.co m...
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.Ite
m("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?

Jul 19 '05 #2

P: n/a
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!
Jul 19 '05 #3

P: n/a
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

"Luke Curtis" <lu*********@westoxon.gov.uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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!

Jul 19 '05 #4

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #5

P: n/a
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

"Luke Curtis" <lu*********@westoxon.gov.uk> wrote in message
news:eJ**************@TK2MSFTNGP12.phx.gbl...
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

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

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.