How to keep Dropdownlist value same after refresh the page
Solution 1
<input type="hidden" name="selectedValue" value="0"/>
put the above one in just below of your select tag
and do this in your servlet
String selectedValue = `request.getParameter("selectedValue")`
now set the selectedValue into the servlet request
create a JS function in your final jsp
function selectedValue(){
var value =<%=request.getParameter("selectedValue")%>;
if(value !=null)
{
document.f1.slvalue.selectedIndex=value ;
}
}
call the selectedValue() function on bodyload of your final jsp page. "slvalue" is a name of your select tag
Solution 2
HTML CODE
<input type=hidden id ="selection" name="selection" value="">
<select name="cate">
<option onclick="document.getElementById('selection').value='h';" value="h">India</option>
<option onclick="document.getElementById('selection').value='m';" value="m">USA</option>
<option onclick="document.getElementById('selection').value='c';" value="c">England</option>
</select>
JSP CODE
<%
String selected = request.getParameter("selection");
%>
Now when you are on the JSP page you have an indication of what the user selected. When you render the element append the select attribute to the selected option
eg: < option value="h" selected > India < / option >
UPDATE In the place where you display the dropdown you have to do some kind of validation where you need to check the selection value and what you are actually printing.
if(selected ==null || selected.equals('')){
//do regular printing of the dropdown as you do now
}else{
out.print("<select>");
while(rs.next()){
if(rs.getString("VALUE").equals(selected )){
out.print("<option value=\""+rs.getString("VALUE")+"\" SELECTED >"+rs.getString("COUNTRY")+"</option>");
}else{
out.print("<option value=\""+rs.getString("VALUE")+"\" >"+rs.getString("COUNTRY")+"</option>");
}
}
out.print("</select>");
}
PS: The code is written on the fly and could need some exception handling or some typos may exist
Prashobh
I am working as a Associate UI consultant in Bangalore(India). My Blog: www.angulartutorial.net
Updated on November 14, 2020Comments
-
Prashobh over 3 years
I have a Dropdownlist
<select name="cate"> <option value="h">India</option> <option value="m">USA</option> <option value="c">England</option> </select> <input type="submit" value="show">
While clicking on show button,in the below table it will show the selected country details. Details are coming from database,and each click the page will refresh.
My problem is that, if a user selects England on Dropdown list and clicks on show button, it will show the details, but the dropdown list will show the first name only. I want to show the selected value name.
Please help me.
-
Prashobh almost 12 yearsthank u,bt it is not working.Clicking on my show button,it will go to servlet,In servlet i written select query,After submission the page will refresh ,then option value will be default one.
-
Anthony Grist almost 12 yearsThe value for the
<select>
(which is the value of the selected option) is already posted with the form (unless it's disabled, but that's not the case here) so saving that value into a hidden input is an unnecessary extra step. -
MaVRoSCy almost 12 years@Anthony Grist This was to indicate an obvious way to get the selected option so that OP could embed it into his code. You are right that it is something unnessesary