Sunday, May 1, 2011

Repopulating database driven textbox on form

Hi,

I have a database driven textbox that needs to be repopulated when the user hits the back button (a back button that I have created on the form)

Currently, I am able to repopulate non database driven fields on the form using http sessions just fine. I cannot seem to apply to same logic to database driven fields.

The code on my jsp looks as follows:

    <td><select name = "actionType" tabindex = "1" value="<%if(session.getAttribute("actionType")== null) out.print(""); else out.print(session.getAttribute("actionType"));%>">
        <option>--</option>

        <% for(int i=0; i<actTypeDDL.size()-1; i++){ 
         String actType = actTypeDDL.get(i).toString();
         i++;
         String actTypeVal = actTypeDDL.get(i).toString();%>

         <option value=<%=actTypeVal%>>
         <%=actType%>
         </option>
         <%
         } %>
        </select></td>

Any ideas?

From stackoverflow
  • The issue doesn't have anything to do with the fact that the value comes from the database, the problem is that specifying a value on a select tag won't preselect the option. You need to add a "selected" flag to the option itself.

    The following should work:

    <td><select name = "actionType" tabindex = "1" >
        <option>--</option>
    
        <% for(int i=0; i<actTypeDDL.size()-1; i++){    
                String actType = actTypeDDL.get(i).toString();
                i++;
                String actTypeVal = actTypeDDL.get(i).toString();%>
    
                <option value=<%=actTypeVal%>
                <% if (session.getAttribute("actionType") == actTypeVal) {
                    System.out.println("selected = 'true'");
                } %>
                >
                <%=actType%>
                </option>
                <%
                } %>
        </select></td>
    
    Ryan Brunner : I'm not sure I understand - your example doesn't have a textbox, only a drop-down. The value would be resubmitted the same as it would the first time - by the select box's value (defined by its selected index) being POSTed to the destination.

0 comments:

Post a Comment