Find your content:

Search form

You are here

Setting a selectoption value on pageblockTable rerender


I have an PageBlockTable which has a wrapper class as its value.

Every time a command button is clicked to add a product, a new row is added to the wrapper class and the PageblockTable is rerendered to display the new row. All these work but when the rerender happens the selectedValue in the selectList is reset to the first item on the selectList.

How can i have the selectList to maintain state?

    <apex:commandButton action="{!addNewProduct}" value="Add New Product" rerender="pbtExistingInstItems,PBCopyProdToInstItems"/>
    <apex:selectList id="slprodname" value="{!itw.installedItem.Product__c}" size="1" rendered="{!itw.isManuallyCreated ==  true}" >
                <apex:selectOptions value="{!prodList}"/>
                <apex:actionSupport event="onchange" action="{!updateDefaultProductValues}"  rerender="colInstallationName">


Attribution to: Prady

Possible Suggestion/Solution #1

It depends on which kind of pagereference you return from your action method addNewProduct

If you are returning full URL then it loads the whole page, that's why it is reloading and reset to first item in select list.

So use only partial page load, return partial URL.

PageReference pr = new PageReference('/apex/PageName'); // for partial page load.

return pr; // use this

PageReference pr = new PageReference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/PageName'); // for full page load.

return pr; //don't use this.

Attribution to: Axaykumar Varu

Possible Suggestion/Solution #2

Just want to explain my solution. Lets say you have some custom object "SomeObject" and a picklist field "ProductList__c" on that, where are all products collected. And you have a wrapper class. All you need is to add a copy of "SomeObject" to every row. And then at your page it could be like this:

<apex:commandButton action="{!addNewProduct}" value="Add New Product" rerender="pbtExistingInstItems,PBCopyProdToInstItems"/>

<apex:inputField value="{!itw.SomeObject.ProductList__c}" rendered="{!itw.isManuallyCreated}" id="slprodname">
    <apex:actionSupport event="onchange" action="{!updateDefaultProductValues}"  rerender="colInstallationName">

The picklist itw.SomeObject.ProductList__c will be rendered as a normal lelect list where selected value will be stored even if you re-renders the pageblock.

Attribution to: Sergej Utko
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content