Preselect a value in a dropdownmenu based on a query-string.

ullenulle
ullenulle used Ask the Experts™
on
Hi there.

I have a SELECT-menu that pulls values from a MySQL-database. I want this particular menu to be populated with a query-string. So far I just added another OPTION VALUE-line, but that's kinda "cheat", since the query-value will be there twice. Any suggestions how to do it more correctly? :-)
Here's my code so far:

<select name="samarbejdspartner_expense" id="samarbejdspartner_expense" onChange="reload()">
     <option value="null" <%If (Not isNull((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then If ("null" = CStr((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%>></option>
     <option value="Request.QueryString("partner")" <%If (Not isNull(Request.QueryString("partner"))) Then If (Request.QueryString("partner") = CStr(Request.QueryString("partner"))) Then Response.Write("selected=""selected""") : Response.Write("")%>><%=(Request.QueryString("partner"))%></option>
     <%
While (NOT Recordset3.EOF)
%>
     <option value="<%=(Recordset3.Fields.Item("partner_navn").Value)%>" <%If (Not isNull((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then If (CStr(Recordset3.Fields.Item("partner_navn").Value) = CStr((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%> ><%=(Recordset3.Fields.Item("partner_navn").Value)%></option>
     <%
  Recordset3.MoveNext()
Wend
If (Recordset3.CursorType > 0) Then
  Recordset3.MoveFirst
Else
  Recordset3.Requery
End If
%>
   </select>

Open in new window


Best regards

Ullenulle
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Big MontyWeb Ninja at large
Commented:
before posting any code, I'd like to make sure I understand what you're trying to accomplish first. Basically, it sounds like you want to default the dropdown to a value based upon a querystring value without having to list that <option> twice, is that correct?
Well, it seems that there is error in second line of your code, please correct:

<option value="<%=Request.QueryString("partner")%>" <%If (Not isNull(Request.QueryString("partner"))) Then If (Request.QueryString("partner") = CStr(Request.QueryString("partner"))) Then Response.Write("selected=""selected""") : Response.Write("")%>><%=(Request.QueryString("partner"))%></option>
Hi guys.

Yes, Big Daddy. That's correct.... and I just messed around with the code on my own and made it work. I attached the code. Thanks for your input. :-)

Best regards

Ullenulle
<select name="samarbejdspartner_expense" id="samarbejdspartner_expense" onChange="reload()">
     <option value="null" <%If (Not isNull((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then If ("null" = CStr((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%>></option>
     <%
While (NOT Recordset3.EOF)
%>
     <option value="<%=(Recordset3.Fields.Item("partner_navn").Value)%>" <%If (Not isNull((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then If (CStr(Recordset3.Fields.Item("partner_navn").Value) = CStr((Recordset1.Fields.Item("samarbejdspartner_expense").Value))) Then Response.Write("selected=""selected""") : Response.Write("") Else If (CStr(Recordset3.Fields.Item("partner_navn").Value) = CStr((Request.QueryString("partner")))) Then Response.Write("selected=""selected""") : Response.Write("")%> ><%=(Recordset3.Fields.Item("partner_navn").Value)%></option>
     <%
  Recordset3.MoveNext()
Wend
If (Recordset3.CursorType > 0) Then
  Recordset3.MoveFirst
Else
  Recordset3.Requery
End If
%>
   </select>

Open in new window

Author

Commented:
I found out the solution on my own... but thanks for your attention. :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial