Forum Moderators: open

Message Too Old, No Replies

How can I retain value of pulldown list in form?

Using ASP, form is submitted to itself and validated

         

The Cricketer

8:46 am on Apr 2, 2004 (gmt 0)

10+ Year Member



I have one .asp page with a SELECT pulldown list on it and some INPUT fields. When SUBMIT is clicked the form data is submitted to that same page and validated. If INPUT fields are empty the asp code will insert a visual indicator (e.g. *) to convey that the field must be filled in.

However, if the user has made a selection in the pulldown list and has left an INPUT field empty, when the form reappears after validation, the pulldown menu looses its selection and returns to its default value.

Would anyone be able to provide a solution to this?
Or do you need to see the code first?

If someone can help I haven't got great asp knowledge so please be as clear as possible.
Thanks a lot.

Equiano

10:55 am on Apr 2, 2004 (gmt 0)

10+ Year Member



Code snippets would help. Notwithstanding the basics of what you need to do are;

Given a simple form as below contained within a page called, say, Foopage.asp


<Form Name="FooForm" Method="Get">
<Select name="FooSelector">
<option Value=0>*</option>
<option Value=1>Foo1</option>
<option Value=2>Foo2</option>
<option Value=3>Foo3</option>
<option Value=4>Foo4</option>
</Select>
<Input Type="Submit"></INPUT>
</FORM>

To Get the ASP page to remember which value was selected , you would need to pass the value back to the page and check for it when the page is being recreated.

Because you've used a GET action for the FORM, all the values you've selected are added to a query string (If used a PUT action then you would need to check the FORM collection).

When FooPage is called after submitting the form, it is called as Foopage.asp?FooSelector=1 (assuming you choose Foo1 from the drop down list before submitting)

To extract the FooSelector value from the Query string at the top your Foopage.asp you would need code of the form


<%
Dim FooSelected
FooSelected = 0

If Request.QueryString("FooSelector") <> "" then
FooSelected = Request.QueryString("FooSelector")
Else
FooSelected = 0
End If
%>

and then amend the form above to be


<Form Name="FooForm" Method="Get">
<Select name="FooSelector">
<%
If FooSelected = 0 then
Response.write "<option Value=0 Selected>*</option>"
Else
Response.write "<option Value=0>*</option>"
End If

If FooSelected = 1 then
Response.write "<option Value=1 Selected>Foo1</option>"
Else
Response.write "<option Value=1>Foo1</option>"
End If
If FooSelected = 2 then
Response.write "<option Value=2 Selected>Foo2</option>"
Else
Response.write "<option Value=2>Foo2</option>"
End If
If FooSelected = 3 then
Response.write "<option Value=3 Selected>Foo3</option>"
Else
Response.write "<option Value=3>Foo3</option>"
End If
If FooSelected = 4 then
Response.write "<option Value=4 Selected>Foo4</option>"
Else
Response.write "<option Value=4>Foo4</option>"
End If
%>

</Select>
<Input Type="Submit"></INPUT>
</FORM>

So after all amendments your Foo.asp page will look like below


<%
Dim FooSelected
FooSelected = 0

If Request.QueryString("FooSelector") <> "" then
FooSelected = Request.QueryString("FooSelector")
Else
FooSelected = 0
End If
%>

<Form Name="FooForm" Method="Get">
<Select name="FooSelector">
<%
If FooSelected = 0 then
Response.write "<option Value=0 Selected>*</option>"
Else
Response.write "<option Value=0>*</option>"
End If

If FooSelected = 1 then
Response.write "<option Value=1 Selected>Foo1</option>"
Else
Response.write "<option Value=1>Foo1</option>"
End If
If FooSelected = 2 then
Response.write "<option Value=2 Selected>Foo2</option>"
Else
Response.write "<option Value=2>Foo2</option>"
End If
If FooSelected = 3 then
Response.write "<option Value=3 Selected>Foo3</option>"
Else
Response.write "<option Value=3>Foo3</option>"
End If
If FooSelected = 4 then
Response.write "<option Value=4 Selected>Foo4</option>"
Else
Response.write "<option Value=4>Foo4</option>"
End If
%>

</Select>
<Input Type="Submit"></INPUT>
</FORM>

This is the basics of whay you need to do.

The Cricketer

1:27 pm on Apr 2, 2004 (gmt 0)

10+ Year Member



Equiano that's a great reply! Thanks very much for your effort. It's helped me think a bit more.

Whilst researching on the internet, I came across a similar solution.

Obviously I haven't told you what my asp code actually looks like, but I think it's also possible to POST the form to itself.

To stop the SELECT list from loosing its selection, I can change the option tag to:

<option value="pink" <% if contactBatsmen1="pink" then %>SELECTED<%end if%>>Pink</option>

Thanks again Equiano

BradleyT

6:17 pm on Apr 2, 2004 (gmt 0)

10+ Year Member



oops, I thought this was asp.net

disregard!