Solved

how to set default value for drop downlist

Posted on 2003-11-13
14
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

First of all let me say that the only language that I speak is English, but in answering questions here I often come across people whose English skills are not the best and I’d like to be able to communicate better with them, and the following descr…
Let's recap what we learned from yesterday's Skyport Systems webinar.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

617 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