Solved

how to set default value for drop downlist

Posted on 2003-11-13
14
324 Views
Last Modified: 2010-04-01
<select name="state">
<option value='red'>RED</option>
<option value='blue'>BLUE</option>
</select>

i mean for text it is like this
<input type="text" name="username" value=<%=vol.getUser()%> >

but how is it for drop down list

thanks
0
Comment
Question by:Jasbir21
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9739306
If you mean that you want (for example) BLUE to be selected in the dropdown by default, use:

<option value='blue' selected>BLUE</option>
0
 

Author Comment

by:Jasbir21
ID: 9743489
actually, the user would select something, and it could either be blue,yellow, red and etc.
On the update page, the default value, value selected by user should be chosen.

Thanks
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9743589
I'm not quite sure why my comment doesn't answer your question.  Unless...  ;-)

To do this programmatically (creating a page when the user has made a selection from the list and this is present in the request):

<%
String[] colorText = {"RED", "BLUE", "YELLOW"};
String[] colorValue = {"red", "blue", "yellow"};
String selectedColor = request.getParameter("state");
%>
<select name="state">
<%
for (int i = 0; i < colorText.length; i++)
{
%>
<option value='<%= colorValue[i] %>'
<%
    if (colorValue[i].equals(selectedColor))
    {
        out.print(" selected");
    }
%>
><%= colorText[i] %></option>
<%
}
%>
</select>

Is this closer ?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Jasbir21
ID: 9743788
sorry, for confusing,
when i put this:

<option value='blue' selected>BLUE</option>
blue is always selected by default, i look up at the dictionary and found out that default is not right word,

A user while entering data could select Red, and in update.jsp, the user would want to view the data entered or changed data, so that value selected by the user which could either be red, orany color is chosen.




0
 
LVL 15

Expert Comment

by:jimmack
ID: 9743841
Sorry Jasbir, I'm still not sure I've got it.  Keep going and we'll get there ;-)

Tell me where I go wrong:

1)  A user has made their color selection on a previous page.
2)  The user then enters the update.jsp page
3)  The server knows about the user's previous color choice.  This could be either:
3.1)  Because update.jsp was called directly from the page where the color selection was made (so it's available in the request)
or
3.2)  Because the previous color selection has been stored in a session variable (or in persistent storage)
4)  The color option list is to be displayed again in the update.jsp page
5)  The previously chosen color is to be selected (highlighted) when the page is displayed.

;-)
0
 

Author Comment

by:Jasbir21
ID: 9743978
1)A user has made their color selection on a previous page.-Yes
2)  The user then enters the update.jsp page-No
3)  The server knows about the user's previous color choice.  This could be either:
3.1)  Because update.jsp was called directly from the page where the color selection was made (so it's available in the request)-No
or
3.2)  Because the previous color selection has been stored in a session variable -Yes(or in persistent storage)
4)  The color option list is to be displayed again in the update.jsp page-Yes
5)  The previously chosen color is to be selected (highlighted) when the page is displayed.
-Yes

Actually, when the value is stored in a session variable, but i don't know how to put the sesion variable in the statement.

For example, for textarea i do like this:
<textarea type="text" name="skill" rows="5" cols="15"  wrap readonly>
<%=vol.getSkill()%>
</textarea>

Thanks
0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 40 total points
ID: 9744006
As before then, but with session.getAttribute() instead of request.getParameter():

You'll need to change "colorselectionattribute" to whatever you used when you did the session.setAttribute().

You'll also need to replace the colorText and colorValue arrays with the source of the RED/red BLUE/blue text and values.

<%
String[] colorText = {"RED", "BLUE", "YELLOW"};
String[] colorValue = {"red", "blue", "yellow"};
String selectedColor = session.getAttribute("colorselectionattribute");
%>
<select name="state">
<%
for (int i = 0; i < colorText.length; i++)
{
%>
<option value='<%= colorValue[i] %>'
<%
    if (colorValue[i].equals(selectedColor))
    {
        out.print(" selected");
    }
%>
><%= colorText[i] %></option>
<%
}
%>
</select>

0
 

Author Comment

by:Jasbir21
ID: 9744132
Hi,
 I think i did something
C:\apache\tomcat\work\Catalina\localhost\jsp-examples\org\apache\jsp\jsp2\volunteer_jsp.java:330: incompatible types
found   : java.lang.Object
required: java.lang.String
String selectedColor = session.getAttribute("state");
                                           ^
1 error


0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 50 total points
ID: 9745337
I guess you are looking at something like this....

<SELECT NAME="GTYPE">
 <OPTION VALUE="TEXT" <%if(test.equals("TEXT")){%>SELECTED=""<%}%>>TEXT</OPTION>
 <OPTION VALUE="LINK" <%if(test.equals("LINK")){%>SELECTED=""<%}%>>LINK</OPTION>
 <OPTION VALUE="MONEY" <%if(test.equals("MONEY")){%>SELECTED=""<%}%>>MONEY</OPTION>
 <OPTION VALUE="DATE" <%if(test.equals("DATE")){%>SELECTED=""<%}%>>DATE</OPTION>
 <OPTION VALUE="MONEY" <%if(test.equals("NUMBER")){%>SELECTED=""<%}%>>NUMBER</OPTION></SELECT>

Here test is the value that you want to match your options with..
It may be retrieved by using
String test=request.getParameter("GTYPE");
or for your case may be by doing
test=vol.getState();

let me know if it works
0
 
LVL 3

Assisted Solution

by:krispols
krispols earned 30 total points
ID: 9745919
hello Jasbir21,

your error is cause you need to cast to String.

String selectedColor = (String)session.getAttribute("state");

But  Kuldeepchaturvedias gave you the good way to do what you expect.

<select name="state">
<option value='red' <% if(session.getAttribute("state").toString.equals("red")) out.print("selected") %>>RED</option>
<option value='blue' <% if(session.getAttribute("state").toString.equals("blue")) out.print("selected") %>>BLUE</option>
</select>

regards
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9746512
Kuldeepchaturvedi and krispols, Thanks for your input but you have both limited the option list to hard coded values.  The colors provided by Jasbir are examples, not necessarily the complete list.

krispols did identify the problem, however, which was the missing cast to (String) from the session.getAttribute() call ;-)
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9746936
The reason it is limited was because of the question was asked looked like a limited options...
Obviously in a real time world you may have the options coming from a database in the form of a resultset...
in this case every hardcoded option can be replaced with rs.getString(1). command...:-)
<%String val =rs.getString(1);%>
<OPTION VALUE=<%=val%><%if(test.equals(val)){%>SELECTED=""<%}%>>TEXT</OPTION>
does that help..........????
0
 

Author Comment

by:Jasbir21
ID: 9747140
hi,
  I tried and three answers solved my problem.So, i think i split points .


Thanks and God bless
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9748656
;-) - Teamwork :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
doubleChar java challenge 7 106
Handle form fields in spring mvc controller 1 64
own marker interface 1 54
listing all functions in JavaScript 19 200
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Learn how the use of a bunch of disparate tools requiring a lot of manual attention led to a series of unfortunate backup events for one company.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now