+ Start a Discussion
SoozeeSoozee 

How to pass a variable from VF page to controller?

Several people have asked this question, and I have tried all of the solutions, but nothing has worked.

 

I want to be able to pass the name of the column header that was clicked on the page into the controller so that I can determine which way to sort (asc vs. desc).

 

Here is my VF page:

<apex:actionFunction name="SortByID" action="{!sortbyRecordID}" rerender="out"/>   

<apex:outputPanel id="out">
<apex:pageBlock >
<apex:pageBlockTable value="{!studentListLongArr}" var="enrollment" rendered="{!EnrollmentListSize > 0}">
<apex:column >
<apex:pageBlockTable value="{!enrollment.Registered_Student__r}" var="stulist">

<apex:column >
<apex:facet name="header">
<apex:commandLink onclick="SortByID();return false;" action="{!doSort}" rerender="out">Record ID
<apex:param name="sortField" value="recordid"/>
</apex:commandLink>
</apex:facet>
<apex:outputLink value="/{!stulist.id}" target="_top">{!stulist.Name}</apex:outputLink>
</apex:column>

 

Here is my controller:

public String direction {get; set;}
public String sortField {public get; public set;}
public String previousSortField {get; set;}

public void doSort(){
direction = 'asc';
//This checks to see if the same header was clicked twice in a row, if so
//it switches the order.
if(previousSortField == sortField){
direction = 'desc';
previousSortField = null;
}else{
previousSortField = sortField;
}
}

public PageReference sortbyRecordID(){

doSort();
System.debug('Value of sortfield is: '+ sortField);
String SOQLQuery1 = 'SELECT id, (Select E.Student_Athlete__c, E.Case__c, E.Number_of_Cases__c, E.Enrollment_Status__c, E.Date_of_Withdrawal__c, E.student__r.name, E.Name, E.Final_Grade__c from Registered_Student__r E LIMIT 5) FROM Course__c where id = :courseId order by id '+ direction;
studentListArr = database.query(SOQLQuery1 );
System.debug('SQL Query1 is :' + SOQLQuery1 );
String SOQLQuery2 = 'SELECT id, (Select E.Student_Athlete__c, E.Case__c, E.Number_of_Cases__c, E.Enrollment_Status__c, E.Date_of_Withdrawal__c, E.student__r.name, E.Name, E.Final_Grade__c from Registered_Student__r E) FROM Course__c where id = :courseId order by id '+ direction;
studentListLongArr = database.query(SOQLQuery2 );
return null;
}

 

 

 

Shashikant SharmaShashikant Sharma

You can use ActionFunction, ActionSupport etc,

 

Read this blog : http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html

 

let me know if any issues in it.

Ispita_NavatarIspita_Navatar

 Hi,

 

You can directly pass that value from VF page to controller using <apex:param> tag assignto prosperities. You have to simply bind that value with the controller variable like this:

 

<apex:page controoler="MyTest">

<apex:form>

<apex:actionFunction name="SortByID" action="{!sortbyRecordID}" rerender="out"/>  

                    <apex:outputPanel id="out">

                                         <apex:pageBlock >                                                                                                                                                                                                                  

                <apex:pageBlockTable value="{!studentListLongArr}" var="enrollment" rendered="{!EnrollmentListSize > 0}">          

                     <apex:column >

                       <apex:pageBlockTable value="{!enrollment.Registered_Student__r}" var="stulist">

                      

                          <apex:column >

                                         <apex:facet name="header">

                                                             <apex:commandLink onclick="SortByID();return false;" action="{!doSort}" rerender="out">Record ID

                                                                                 <apex:param name="sortField" value="{!stulist.Name}" assignTo="{!TestName}"/>

                                                             </apex:commandLink>

                                         </apex:facet>

                                         <apex:outputLink value="{!stulist.id}" target="_top">{!stulist.Name}</apex:outputLink>

                          </apex:column>

</apex:form>                                                                                                                   

</apex:page>                                                                                             

///////////////////////////// Controller /////////////////////////////////////////

 

public class MyMyTest()

{

                    public string TestName {get;set;}

                                         public MyMyTest()

                    {

                    system.debug('@@@@@@@@@@@@@@' +TestName);

                    //now you will get the value fo TestName which you are passing from VF param tag.

                    }

                    // Put your desire code here now.

 

}

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

SoozeeSoozee

Thanks, I added the assignTo value, but I am still having trouble.

 

The value is never passed to the controller.  This is what is written to the log:

"09:34:59:026 USER_DEBUG [67]|DEBUG|Value of sortfield is: null"