Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting selected value in a drop-down box to value of Session variable 1

Status
Not open for further replies.

itlee

Programmer
Feb 14, 2001
34
CH
Hi,

I have a form with a drop-down box, when the form is submitted the selected value is assigned to a Session variable, which is then used in the next asp page.

When the user returns to the original form I want the drop-down box to set the selected value to that of the value from the Session variable.

Any ideas on how I can do this?

Also I want to do a similar thing with a radio button.

Lee.

ITLee. Dev\Prog\DBA\ITIL
 
If your dropdown list is populated by going through a loop then you can do this easily like this:
Code:
While NOT objRS.EOF
  intID = objRS("id")
  strText = objRS("sometext")

  If strText = Session("somevalue") Then
    strSelected = " SELECTED"
  Else
    strSelected = ""
  End If

  Response.Write "<OPTION VALUE='" & intID & "'" & strSelected & ">" & strText & "</OPTION>" & vbcrlf
  objRS.MoveNext
Wend

Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
I see where you are coming from, but I am not using a recordset to populate the drop-down box, its hardcoded.

Code:
<select size="1" name="cboServiceGroup">
<option value="ALL">All PinkRoccade Groups</option>
        <option value="COMP">Core Groups</option>
        <option value="TS">Contact Centre Groups</option>
        <option value="TMI">T-Mobile International</option>
        <option value="BF">Distributed Services</option>
        <option value="NONE">None</option>
</select>

I am trying to do it inline, something like:

Code:
<select size="1" name="cboServiceGroup">
<option value="ALL">All PinkRoccade Groups</option>
        <option <%=If Session("Choice") = "COMP" Then " selected" End If %>value="COMP">Core Groups</option>

etc...


ITLee. MCP\Dev\Prog\DBA\ITIL
 
try this then...
Code:
<option <%=If Session("Choice") = "COMP" Then [b]Response.Write[/b] " selected" End If %>value="COMP">Core Groups</option>

Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
I am getting this error!

Microsoft VBScript compilation error '800a03ea'

Syntax error

/ToBePublished/CustomerCare/Default.asp, line 49

Response.Write(If Session("SrvcGrp") = "ALL" Then Response.Write " selected " End If)
---------------^


the page is an asp page, but the html is written as html not as response.write(html). Is this occurring because the asp parser reads the html then uses response.write to write the page to the browser? or is there something else I am missing?

Lee.

ITLee. MCP\Dev\Prog\DBA\ITIL
 
You may be better just displaying the list and then using some Javascript such as the following to do this for you :

<script language="javascript">
var selectedElement="<%=Session("SrvcGrp")%>";
for (count=0;count<document.myForm.cboServiceGroup.options.length;count++)
{
if (document.myForm.cboServiceGroup.options[count].value==selectedElement)
{
document.myForm.selectedElement.options[count].selected=true;
}
}
</script>
 
From your original code:

<option <%=If Session("Choice") = "COMP" Then " selected" End If %>value="COMP">Core Groups</option>

To work this way it should be:

<option <%=If Session("Choice") = "COMP" Then%> selected <% End If %>value="COMP">Core Groups</option>

Fester's way will definitely work. It looks like you have the cart before the horse. Try getting rid of the first Response.Write:

<%=If Session("SrvcGrp") = "ALL" Then Response.Write " selected " End If%>


Wow JT that almost looked like you knew what you were doing!
 
there is no response.write on line 49, its html with the asp inline:

Code:
<select size="1" name="cboServiceGroup">
<option [COLOR=red]<%=If Session("SrvcGrp") = "ALL" Then%>[/color] selected [COLOR=red]<% End If %>[/color] value="ALL">All PinkRoccade Groups</option>
<option [COLOR=red]<%=If Session("SrvcGrp") = "COMP" Then Response.Write(" selected ") End If %>[/color] value="COMP">Core Groups</option>
<option [COLOR=red]<%=If Session("SrvcGrp") = "TS" Then Response.Write(" selected ") End If %>[/color]value="TS">Contact Centre Groups</option>
<option [COLOR=red]<%=If Session("SrvcGrp") = "TMI" Then Response.Write(" selected ") End If %>[/color]value="TMI">T-Mobile International</option>
<option [COLOR=red]<%=If Session("SrvcGrp") = "BF" Then Response.Write(" selected ") End If %>[/color]value="BF">Distributed Services</option>
<option [COLOR=red]<%=If Session("SrvcGrp") = "NONE" Then Response.Write(" selected ") End If%>[/color]value="NONE">None</option>
</select>

As you can see I modified the first option to pixl8r's suggestion and still the error occurs with both methods. Please no suggestions in java as I don't know java.

Cheers

ITLee. MCP\Dev\Prog\DBA\ITIL
 
Cheers FestersSXS, star awarded to you.

ITLee. MCP\Dev\Prog\DBA\ITIL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top