Outputlink vs Commandlink

Outputlink vs Commandlink

Apex:outputLinkA link to a URL. This component is rendered in HTML as an anchor tag with an href attribute. Like its HTML equivalent, the body of an <apex:outputLink> is the text or image that displays as the link. To add query string parameters to a link, use nested <apex:param> components.

<apex:outputlink value="https://www.google.co.in"> Google </apex:outputlink>

<apex:outputlink value="/{!r.Id}"> {!r.Name} </apex:outputlink>

apex:commandLinkA link that executes an action defined by a controller, and then either refreshes the current page, or navigates to a different page based on the PageReference variable that is returned by the action.
 An <apex:commandLink> component must always be a child of an <apex:form> component.
To add request parameters to an <apex:commandLink>, use nested <apex:param> components.

<apex:commandLink value="Google" action="https://www.google.co.in"/>

<apex:commandLink value="Delete" action="{!dodelete}">
      <apex:param name="eid" value="{!E.Id}" assignTo="{!rId}"/>
</apex:commandLink>

public pagereference doDelete(){
 delete [select id from Employee__c where id =: rid];
 return(new pagereference ('/apex/commandlink1').setredirect(true));
    }

<apex:commandLink value="Show Data" action="{!doShow}">
   <apex:param name="aid" value="{!A.Id}" assignTo="{!rId}"/>
</apex:commandLink>
public PageReference doShow() {

//return(new pagereference('/apex/showPage?id='+rId).Setredirect(true));

    pagereference ref = new pagereference('/apex/showPage?id='+rId);
    ref.setredirect(true);
    return ref;
    }


Outputlink with parameters

  1. <apex:page standardController="Contact">
  2.     <apex:outputLink value="http://google.com/search">
  3.         Search Google
  4.         <apex:param name="q" value="{!contact.name}"/>
  5.     </apex:outputLink>
  6. </apex:page>
  









Example 2:


  1. <apex:page standardController="Account" recordSetVar="records">
  2.   <apex:form >
  3.       <apex:pageBlock >
  4.           <apex:pageBlockTable value="{!records}" var="r">
  5.               <apex:column headerValue="Account Name" >
  6.                   <apex:outputlink value="/{!r.Id}"> {!r.Name} </apex:outputlink>
  7.               </apex:column>
  8.               <apex:column headerValue="Account Phone" value="{!r.Phone}"/> 
  9.           </apex:pageBlockTable>
  10.       </apex:pageBlock>
  11.   </apex:form>
  12. </apex:page>
On clicking the account name the account detail page will open as the account name is hyper linked with its record id in apex:outputlink statement in vf page





Delete record using commandlink

  1. <apex:page controller="CommandLinkCls">
  2.  <apex:form >
  3.      <apex:pagemessages ></apex:pagemessages>
  4.      <apex:pageBlock rendered="{!IF(lstE.size ==0,false,true)}">
  5.      
  6.          <apex:pageBlockTable value="{!lstE}" var="E">
  7.              <apex:column headerValue="Action">
  8.                  
  9.       <apex:commandLink value="Delete" action="{!dodelete}">
  10.    <apex:param name="eid" value="{!E.Id}" assignTo="{!rId}"/>
  11.       </apex:commandLink>
  12.              
  13.              </apex:column>
  14.              <apex:column headerValue="Employee ID" value="{!E.Name}"/>
  15.              
  16.          </apex:pageBlockTable>
  17.      </apex:pageBlock>
  18.      
  19.  </apex:form>
  20. </apex:page>
  1. public with sharing class CommandLinkCls {

  2.     public PageReference doshow() {
  3.         return null;
  4.     }


  5.     public List<Employee__c> lstE{get;set;}
  6.    
  7.     public CommandLinkCls(){
  8.     lstE = new List<Employee__c>();
  9.     lstE = [select id,name,Email_Address__c,Employee_Name__c from Employee__c];
  10.                                 
  11.     if(lstE.size() == 0){
  12.     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Data not found.............');
  13.                                     apexpages.addMessage(myMsg);
  14.                                 }
  15.                                 
  16.                             }
  17.     
  18.     public String rId{get;set;}
  19.     
  20.     public pagereference doDelete(){
  21.         delete [select id from Employee__c where id =: rid];
  22.         return (new pagereference('/apex/commandlink1').setredirect(true));
  23.     }
  24. }






Redirecting to other page using commandlink 


  1. <apex:page showHeader="false" controller="ShowDataclass">
  2.  <apex:form >
  3.      <apex:pageBlock >
  4.          <apex:pageBlockTable value="{!lstA}" var="A">
  5.              <apex:column headerValue="Action">
  6.                  <apex:commandLink value="{!A.Name}" action="{!doShow}">
  7.                      <apex:param name="aid" value="{!A.Id}" assignTo="{!rId}"/>
  8.                  </apex:commandLink>
  9.              </apex:column>
  10.              <apex:column headerValue="Account Name" value="{!A.Name}"/>
  11.          </apex:pageBlockTable>
  12.      </apex:pageBlock>
  13.  </apex:form>
  14. </apex:page>
  1. public with sharing class ShowDataclass {
  2.      public String rId{get;set;}
  3.      public List<Account> lstA{get;set;}
  4.       public ShowDataclass(){
  5.         lstA = new List<Account>();
  6.         lstA = [select id,name,phone from Account];
  7.        }
  8.     public PageReference doShow() {
  9.         //return (new pagereference('/apex/showPage?id='+rId).setredirect(true));
  10.         pagereference ref = new pagereference('/apex/showPage?id='+rId);
  11.         ref.setredirect(true);
  12.         return ref;
  13.     }  
  14. }
showPage
  1. <apex:page showHeader="false" controller="ShowDataDisplayCls">
  2.   <apex:form >
  3.       <apex:pageBlock >
  4.           <apex:pageblocktable value="{!lstA}" var="A">
  5.               <apex:column headerValue="Account Name" value="{!A.Name}"/>
  6.               <apex:column headerValue="Account Phone" value="{!A.Phone}"/>
  7.           </apex:pageblocktable>
  8.       </apex:pageBlock>
  9.   </apex:form>
  10. </apex:page>
  1. public with sharing class ShowDataDisplayCls {

  2.     public List<Account> lstA{get;set;}
  3.     public ShowDataDisplayCls(){                        //Constructor 
  4.         lstA = new List<Account>();
  5.         
  6.         lstA = [select id, name, phone from Account where id = :apexpages.currentpage().getparameters().get('id')];
  7.     }
  8. }

Comments

Popular Posts