Dynamically selecting a value in a listbox from a passed variable

Discussion in 'ASP General' started by Lukelrc, Jul 12, 2004.

  1. Lukelrc

    Lukelrc Guest

    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?
     
    Lukelrc, Jul 12, 2004
    #1
    1. Advertising

  2. Lukelrc

    Ray at Guest

    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" <> wrote in message
    news:...
    > 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?
     
    Ray at, Jul 12, 2004
    #2
    1. Advertising

  3. Lukelrc

    Luke Curtis Guest

    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!
     
    Luke Curtis, Jul 12, 2004
    #3
  4. Lukelrc

    Ray at Guest

    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" <> wrote in message
    news:%...
    > 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!
     
    Ray at, Jul 12, 2004
    #4
  5. Lukelrc

    Luke Curtis Guest

    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!
     
    Luke Curtis, Jul 13, 2004
    #5
  6. Lukelrc

    Ray at Guest

    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" <> wrote in message
    news:...
    > 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!
     
    Ray at, Jul 14, 2004
    #6
    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. dee
    Replies:
    9
    Views:
    517
    Joseph Byrns
    Apr 15, 2005
  2. Anand
    Replies:
    2
    Views:
    908
    Anand
    Sep 11, 2003
  3. Roger
    Replies:
    1
    Views:
    725
    Roger
    Jan 10, 2007
  4. LaundroMat
    Replies:
    50
    Views:
    967
    Hendrik van Rooyen
    Oct 14, 2006
  5. Allen
    Replies:
    6
    Views:
    322
    Default User
    Feb 6, 2007
Loading...

Share This Page