2

This is my VF Page

<apex:page controller="SearchFromURLController">
<apex:pageblock title="Account Search">
<apex:form >
<apex:pageblocksection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Enter the search string : " />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:inputText value="{!name}"/>
</apex:pageBlockSectionItem>
<apex:commandButton status="id_stat" value="Go" action="{!popAccounts}"/>
<!--NOTE : adding attribute rerender="results_pb,no_recs" in the above cmdbtn cause no records to be shown-->
</apex:pageblocksection>
<apex:actionstatus starttext="Please wait..." id="id_stat" />
</apex:form>
</apex:pageblock>
<apex:pageblock rendered="{!searched}"  title="Results" id="results_pb" >
<apex:pageBlockTable value="{!accounts}" var="a">
<apex:column value="{!a.Name}"/>
<apex:column value="{!a.Industry}" />
<apex:column value="{!a.Type}" />
</apex:pageBlockTable>
</apex:pageblock>
<apex:pageblock id="no_recs" rendered="{!isZero}">
<apex:outputtext value="No Records Found"/>
</apex:pageblock>
</apex:page>

This is my controller

public with sharing class SearchFromURLController {

public String name{get;set;}
public Boolean searched{get;set;}
public Boolean IsZero{get;set;}

public List<Account> accounts{get;set;}

public PageReference popAccounts()
{
   isZero = false;

   if(name != null)
   {
       searched = true;
       String strquery = '%'+name+'%';
       accounts = [SELECT Id,Name,Industry,Type FROM Account WHERE Name like :strquery];
       if(accounts.size() == 0)
       {
         isZero = true;
       }
       System.debug('~~~~ '+isZero);
       System.debug('~~~~~ '+accounts.size());
       System.debug('~~~~~ '+strquery);

   }
   return null;


}
}

This VF page will contain a text box and a command button.

The user will search for any string and it will show all the accounts where the name contains the searched string.

I observed that if I keep rerender on commandbutton to either the results table("results_pb") or to the pageblock("no_recs") nothing is rendered in the VF page.

But if I remove them, the result records are shown as expected.

Can someone let me know why would no records be displayed if we "rerender" the pageblock or the data table after clicking the command button ?

1 Answer 1

4

It is a best practice to keep element using rendered attribute and the element being re-rendered as different.

The simple reason for it being that the element which is marked as rendered="false" is not visible on the page source.

In your case, the following lines make the element invisible and pretty much undetectable:

<apex:pageblock rendered="{!searched}"  title="Results" id="results_pb" >

<apex:pageblock id="no_recs" rendered="{!isZero}">

When the command button tries to re-render these, it isn't possible as they do not exist on the page.

The solution for this is to take an outputPanel outside these pageBlock elements and assign them the ids. That way the outputPanels will be there on the page and could be detected by the command button.

The pageBlocks inside will be rendered based on the values provided.

Hope this helps.

2
  • "It is a best practice to keep element using rendered attribute and the element being re-rendered as different. "..This cleared my query...thanks a lot !!! Apr 21, 2015 at 0:34
  • Glad I could help :) Apr 21, 2015 at 5:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .