Solved

how to set default value for drop downlist

Posted on 2003-11-13
14
327 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
5 Ways to automate alert management in ConnectWise // The art of alerting is in automation. When humans are removed from the alerting process, errors are fewer and action is almost immediate. But how do you automate the alerting process and what …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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