function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AFawcett (CODA)AFawcett (CODA) 

View State changes not getting saved?

I've written an action handler method, that modifies my view state (member variable in my controller) and I would like to redisplay the screen to show the changes. Why doesn't this show "Hello World" when a click the button? Am I expecting something outside the design, I've studied the wizard sample and this works due to the fact it seems to be changing pages in the event handler. Thanks for any help.

<apex:page controller="HelloWorldController">

<apex:form>

<apex:pageBlock title="Hello {!$User.FirstName}!">

<apex:inputTextarea></apex:inputTextarea> value="{!message}"/> <p/>

<apex:commandButton action="{!showmessage}" value="Show Message"/>

</apex:pageBlock>

</apex:form>

</apex:page>

 

public class HelloWorldController

{

String message;

public String getMessage()

{

return message;

}

public PageReference showMessage()

{

message = 'Hello World';

return null;

// Also tried this...no joy either

// return System.currentPageReference();

}

}

AFawcett (CODA)AFawcett (CODA)
The Visual Force page got a bit corrupted for some reason, please see this one.
 
<apex:page controller="HelloWorldController">
<apex:form>
<apex:pageBlock title="Hello {!$User.FirstName}!">
<apex:inputText value="{!message}"/><p/>
<apex:commandButton action="{!showmessage}" value="Show Message"/>
</apex:pageBlock>
</apex:form>
</apex:page>
dchasmandchasman
The problem is a bit on the subtle side (a fix for that lack of error message has already been added for Winter '08) and is caused by binding an input component (apex:inputTextarea in this case) to a read only property in your controller. In Winter '08 you'll get this error message when you try to save the page:

   Error: Read only property 'HelloWorldController.message

you can correct this by adding:

   public
void setMessage(String message) {   
      this
.message = message;
   }

to your controller.

Message Edited by dchasman on 10-03-2007 09:09 PM

AFawcett (CODA)AFawcett (CODA)

That worked thanks, however my demo was a little to simplistic. This is more representative of what I am trying to do. If I have the getLines method always return null Show Messages works, if it returns a new List it doesn't. Is there some issue here with the serailisation of the view state?

Basically the "Add Line" button doesn't work as I was expecting?

public class HelloWorldController

{

String message;


List<CODADocumentViewStateLine> lines;

public String getMessage()

{

return message;

}


public void setMessage(String value)

{

message = value;

}


public List<CODADocumentViewStateLine> getLines()

{

if(lines==null)

{

lines = new List<CODADocumentViewStateLine>();

lines.add(new CODADocumentViewStateLine());

}

return lines;

}

public PageReference showMessage()

{

message = 'Hello World';

return null;

}


public PageReference addLine()

{

lines.add(new CODADocumentViewStateLine());

return null;

}

}

 

<apex:form>

<apex:pageBlock title="Hello {!$User.FirstName}!">

<apex:inputText value="{!message}"/><p/>

<apex:commandButton action="{!showmessage}" value="Show Message"/><p/>

<apex:commandButton action="{!addline}" value="Add Line"/><p/>

<apex:dataTable value="{!lines}" rows="50" var="linestmp" styleClass="list" rowClasses="dataRow" onRowMouseOver="hiOn(this);" onRowMouseOut="hiOff(this);">

<apex:column>

<apex:facet name="header"><b>Line</b></apex:facet>

{!linestmp.name}

</apex:column>

<apex:column>

<apex:facet name="header"><b>Description</b></apex:facet>

<apex:inputText value="{!linestmp.description}"/>

</apex:column>

<apex:column>

<apex:facet name="header"><b>Nominal</b></apex:facet>

<apex:inputText value="{!linestmp.nominal}"/>

</apex:column>

<apex:column>

<apex:facet name="header"><b>Product</b></apex:facet>

<apex:inputText value="{!linestmp.product}"/>

</apex:column>

<apex:column>

<apex:facet name="header"><b>Account</b></apex:facet>

<apex:inputText value="{!linestmp.account}"/>

</apex:column>

<apex:column>

<apex:facet name="header"><b>Value</b></apex:facet>

<apex:inputText value="{!linestmp.value}"/>

</apex:column>

</apex:dataTable>

</apex:pageBlock>

</apex:form>

</apex:page>

 

public class CODADocumentViewStateLine
{
 String m_name;
 String m_nominal;
 String m_account;
 String m_product;
 String m_description;
 double m_value;
 
 public String getName()
 {
  return m_name;
 }
 
 public String getNominal()
 {
  return m_nominal;
 }
 
 public String getAccount()
 {
  return m_account;
 }
 
 public String getProduct()
 {
  return m_product;
 }
 
 public String getDescription()
 {
  return m_description;
 }
 
 public double getValue()
 {
  return m_value;
 }
 
 public void setNominal(String value)
 {
  m_nominal = value;
 }
 
 public void setAccount(String value)
 {
  m_account = value;
 }
 
 public void setProduct(String value)
 {
  m_product = value;
 }
 
 public void setDescription(String value)
 {
  m_description = value;
 }
 
 public void setValue(double value)
 {
  m_value = value;
 }   
}
dchasmandchasman
OK - the source of the problem is the use of a non-String data type for a setter method
(CODADocumentViewStateLine.value in this case).  If you change from Double to String you'll be back in business.

This issue has already been fixed in Winter '08.
AFawcett (CODA)AFawcett (CODA)
This is great, thanks very much.