+ Start a Discussion
Shephali SwarnkarShephali Swarnkar 

how to open a pageblock on button click

Hi All,
   Below is my code.Here i want to show the record on button click.but its not working.
<!--VF Code-->
<apex:page controller="Contacts">
<apex:form >
<apex:commandButton action="{!processSelected}" value="Process Selected" reRender="pageBlock, pageBlockProcessed" status="AjaxStatus">
</apex:commandButton>
<apex:outputPanel id="thePanel">
 <apex:pageBlock title="Selected Records" id="pageBlockProcessed">
    <apex:pageBlockTable value="{!selectedContacts}" var="l">
     <apex:column value="{!l.Name}"></apex:column>
     <apex:column value="{!l.Email}"></apex:column>
      <apex:column value="{!l.Phone}"></apex:column>
      </apex:pageBlockTable>
       </apex:pageBlock>
       </apex:outputPanel>
       </apex:form>
       </apex:page>

//Controller code
public class Contacts 
{
  public list<contact> selectedContacts{set;get;}
    public void processSelected()
     {
        selectedContacts=[select Name, Email, Phone from contact];        
    }

}
KaranrajKaranraj
What error message you are getting? I don't see anyproblem in the code and its working as expeced. When I click the 'Process Selected' button then its display the contact records in a pagebloack table. Make sure that you have contact records in your org.
<apex:page controller="Contacts">
<apex:form >
<apex:commandButton action="{!processSelected}" value="Process Selected" reRender="thePanel">
</apex:commandButton>
<apex:outputPanel id="thePanel">
 <apex:pageBlock title="Selected Records" id="pageBlockProcessed">
    <apex:pageBlockTable value="{!selectedContacts}" var="l">
     <apex:column value="{!l.Name}"></apex:column>
     <apex:column value="{!l.Email}"></apex:column>
      <apex:column value="{!l.Phone}"></apex:column>
      </apex:pageBlockTable>
       </apex:pageBlock>
       </apex:outputPanel>
     </apex:form>
 </apex:page>




 
Gyanender SinghGyanender Singh
Hi Shephali Swarnkar,
As i check your code, your code was fine and it was working but in the reRender Part of the command button there is no need to reRender pageblock because for reRender you have to mention the id but you was mention the pageblock name and the another thing is that there was no need of output panel in your page.

<apex:page controller="ContactsController">
    <apex:form >
        <apex:commandButton action="{!processSelected}" value="Process Selected" reRender="pageBlockProcessed" status="AjaxStatus"/>
            <apex:pageBlock title="Selected Records" id="pageBlockProcessed">
                <apex:pageBlockTable value="{!selectedContacts}" var="l">
                    <apex:column value="{!l.Name}"></apex:column>
                    <apex:column value="{!l.Email}"></apex:column>
                    <apex:column value="{!l.Phone}"></apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
    </apex:form>
</apex:page>


public class ContactsController {
    public list<contact> selectedContacts{set;get;}
    public void processSelected()
     {
        selectedContacts=[select Name, Email, Phone from contact];        
    }
}
Shephali SwarnkarShephali Swarnkar
Hi Karanraj and Gyanendra,
   Thanks for your reply.its working now. As Gyanendra metioned and karan you too corrected that code is fine the problem was with reRender part only.
Thanks to both of you again.