aspose file tools*
The moose likes JSP and the fly likes select comes to default again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "select comes to default again" Watch "select comes to default again" New topic
Author

select comes to default again

Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
hi i have a made a page with 5 dropdown box/select now i have done that on selection of 1st select 2nd is populated but the problem exists that as soon as i change select option my 2nd select is populated but the value of 1st select is rest to default value

what should i do my code for this page is:-



please help me

thank you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

As always, step one is to refactor the JSP to move all Java code out of the JSP and use the JSTL and EL instead.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
sorry bear i will remember it in future
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

When the page is reloaded after the submit, any previous state is lost. If you want the first dropdown to retain its value, you need to put the selected attribute on the option to be initially selected.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
hey Bear thanks for your reply but when i kept a selected value as default then my select donot accept that value in further cases

what should ii do then
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Aditi agarwal wrote:hey Bear thanks for your reply but when i kept a selected value as default then my select donot accept that value in further cases

I do not know what you mean by this. If you correctly place the selected attribute on an option element, it will be the default selected option.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
sorry as i canot explain myself to you
let us suppose my select has 4 values

IC
Marketing
Economic
Refinery

no i need when i select marketing it should remain there & longwith it mmy 2nd select should also get fill

my 2nd select is getting filled but marketing is not shown to me in 1st instead "select is shown"

as per you if i make my IC as default first value then whenever i click on IC it will not be taken as any value means 2nd select is not filled





i think if i undesrtand what you are saying is this then et me kow any other solution for this please

thank you bear
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Basic HTML Lesson #4678: The "selected" attribute of the option tag.
When you create a list of options for a select control, you can choose one of them to be selected.

<option value="foo" selected>

So the trick is when you re-render the page, and re-create the options for the first dropdown, you need to mark one of them as "selected"
Preferably the one which was chosen by the user.
In your code it would need something like this:



So for most of the options, it will generate what it already does.
For the option the user selected, it should add the "selected" attribute in - meaning that item will be selected when you render the page.


Of course this is all awful scriptlet code which doesn't belong on a JSP.
A basic framework would take care of this for you.

Most of them will let you do something like the following:


This declares a select box with the name "division"
Creates options based on a List of options you pass it.
Will automagically create the selected attribute on the relevant option.

But it takes all of that crap scriptlet code out of your JSP. Doesn't it look much neater like that?

The standard suggestions that if you've looked at any post in this forum you will know:
- Don't use scriptlet code in JSP. Java code belongs in a java class.
- SQL does not belong on a JSP page.
Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Hi,

You may try the below option.. create one dummy hidden field in JSP after that while on changing the dropdown value we can assign the value to that hidden variable using Javascript. then during the reload of the jsp page we can reassign the hidden variable value to dropdown object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: select comes to default again