This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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.
Joined: Feb 23, 2011
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
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.