Solved

how to set default value for drop downlist

Posted on 2003-11-13
14
319 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

12 Experts available now in Live!

Get 1:1 Help Now