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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Asp - List control

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I have the following codes...

<select name=&quot;SrcSelec&quot; size=&quot;1&quot;>
<% Dim Rs
Set Rs = obj.GetId ()
Do While not Rs.eof %>
<option value=&quot;<% =Rs(&quot;id&quot;)%>&quot;><%Rs(&quot;desc&quot;)% ></option>
<% Rs.movenext %>
<% Loop %>
</select>


For instance, it's going to load:
1 - cat
2 - dog
3 - horse
etc ...

I'd like it to defaut to a value of 3. How can I do this?

Thanks.


 
You could try:


<% Dim Rs
Set Rs = obj.GetId ()
Do While not Rs.eof %>
<%If Rs(&quot;id&quot;) = 3 Then%>
<option selected value=&quot;<%=Rs(&quot;id&quot;)%>&quot;><%=Rs(&quot;desc&quot;)% ></option>
<%Else%>
<option value=&quot;<%=Rs(&quot;id&quot;)%>&quot;><%=Rs(&quot;desc&quot;)%></option>
<%End If%>
<%Rs.movenext %>
<% Loop %>


I think that would do what you are asking....
 
You could use javascript.
<script language=&quot;javascript&quot;>
function setSelect(){
document.formname.SrcSelect.value=&quot;3&quot;
}
</script>
<body onLoad=&quot;setSelect()&quot;>

Using this the java will make option 3 the selected option.

Roj
 
to take jitter's solution and whittle it down just a tad:

<%Dim Rs
Set Rs = obj.GetId ()
Do While not Rs.eof
Response.Write &quot;<option value=&quot;&quot;&quot; & Rs(&quot;id&quot;) & &quot;&quot;
If Rs(&quot;id&quot;) = 3 Then
Response.Write &quot; selected&quot;
End If
Response.Write &quot;>&quot; & Rs(&quot;desc&quot;) & &quot;</option>&quot;
Rs.movenext
Loop %>
 
Thanks lobstah. Did not think about doing it like that. I just love these forums....:)
 
I prefer mixing JavaScript and VBScript together like yandso. It allows you to set the box if aren't writing out the options in ASP.

<% SelectedValue = rs(&quot;State&quot;) %>
<html>
<head>
<script language=&quot;javascript&quot;>
function setSelect(){
document.formname.SrcSelect.value=&quot;<%=SelectedValue%>&quot;;
}
</script>
</head>
<body onLoad=&quot;setSelect()&quot;>
<form name=&quot;formname&quot;>
<select name=&quot;SrcSelect&quot;>
<option value=&quot;MO&quot;>Missouri
<option value=&quot;TX&quot;>Texas
<!--Rest of the states-->
</select>
</form>

Say for instance I have a list of cities, and I want MO selected. This works great if I am not creating the list in ASP. I can't check each option to see if it's the correct one because I'm not writing them out one by one. I'd still like the state to be selected, and I don't just want to do this by writing out a new option tag at the top with the right one. Then I end up with two option tags for MO, which is confusing if someone tries to change the state and bad programming.

Hope that helps. Harold Blackorby
hblackorby@scoreinteractive.com
St. Louis, MO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top