Outputlink vs Commandlink
Outputlink vs Commandlink
Apex:outputLink – A 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:commandLink – A 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
- <apex:page standardController="Contact">
- <apex:outputLink value="http://google.com/search">
- Search Google
- <apex:param name="q" value="{!contact.name}"/>
- </apex:outputLink>
- </apex:page>


Example 2:
- <apex:page standardController="Account" recordSetVar="records">
- <apex:form >
- <apex:pageBlock >
- <apex:pageBlockTable value="{!records}" var="r">
- <apex:column headerValue="Account Name" >
- <apex:outputlink value="/{!r.Id}"> {!r.Name} </apex:outputlink>
- </apex:column>
- <apex:column headerValue="Account Phone" value="{!r.Phone}"/>
- </apex:pageBlockTable>
- </apex:pageBlock>
- </apex:form>
- </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
- <apex:page controller="CommandLinkCls">
- <apex:form >
- <apex:pagemessages ></apex:pagemessages>
- <apex:pageBlock rendered="{!IF(lstE.size ==0,false,true)}">
- <apex:pageBlockTable value="{!lstE}" var="E">
- <apex:column headerValue="Action">
- <apex:commandLink value="Delete" action="{!dodelete}">
- <apex:param name="eid" value="{!E.Id}" assignTo="{!rId}"/>
- </apex:commandLink>
- </apex:column>
- <apex:column headerValue="Employee ID" value="{!E.Name}"/>
- </apex:pageBlockTable>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
- public with sharing class CommandLinkCls {
- public PageReference doshow() {
- return null;
- }
- public List<Employee__c> lstE{get;set;}
- public CommandLinkCls(){
- lstE = new List<Employee__c>();
- lstE = [select id,name,Email_Address__c,Employee_Name__c from Employee__c];
- if(lstE.size() == 0){
- ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Data not found.............');
- apexpages.addMessage(myMsg);
- }
- }
- public String rId{get;set;}
- public pagereference doDelete(){
- delete [select id from Employee__c where id =: rid];
- return (new pagereference('/apex/commandlink1').setredirect(true));
- }
- }
Redirecting to other page using commandlink
- <apex:page showHeader="false" controller="ShowDataclass">
- <apex:form >
- <apex:pageBlock >
- <apex:pageBlockTable value="{!lstA}" var="A">
- <apex:column headerValue="Action">
- <apex:commandLink value="{!A.Name}" action="{!doShow}">
- <apex:param name="aid" value="{!A.Id}" assignTo="{!rId}"/>
- </apex:commandLink>
- </apex:column>
- <apex:column headerValue="Account Name" value="{!A.Name}"/>
- </apex:pageBlockTable>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
- public with sharing class ShowDataclass {
- public String rId{get;set;}
- public List<Account> lstA{get;set;}
- public ShowDataclass(){
- lstA = new List<Account>();
- lstA = [select id,name,phone from Account];
- }
- 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;
- }
- }
showPage
- <apex:page showHeader="false" controller="ShowDataDisplayCls">
- <apex:form >
- <apex:pageBlock >
- <apex:pageblocktable value="{!lstA}" var="A">
- <apex:column headerValue="Account Name" value="{!A.Name}"/>
- <apex:column headerValue="Account Phone" value="{!A.Phone}"/>
- </apex:pageblocktable>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
- public with sharing class ShowDataDisplayCls {
- public List<Account> lstA{get;set;}
- public ShowDataDisplayCls(){ //Constructor
- lstA = new List<Account>();
- lstA = [select id, name, phone from Account where id = :apexpages.currentpage().getparameters().get('id')];
- }
- }
Comments
Post a Comment