+ Start a Discussion
kgrasukgrasu 

Why am I unable to retrieve the value of Apex:param in my controller?

Hi,
 
I have a very basic question in Visualforce. I need to have a parameter in the VF page, assign a value to it using javascript and then retrieve the parameter values from the controller.
 This is my part VF code:
 
Code:
<apex:page controller="conLeadTab" action="{!validate}">
<apex:pageBlock id="mypageblock" mode="new">
                 <apex:param id="vesg"   name="vesg" value=""/>
     </apex:pageBlock>
<script language="javascript">
var vesgval = 'SOme value';
//The line below does not work, but here I need to assign the value of vesgval to the parameter vesg
//{!$CurrentPage.parameters.vesg}=vesgval;
</script>  
</apex:page> 

 

   
//My controller method 
 
Code:
  public PageReference validate() {
      this.pesgval = System.currentPageReference().getParameters().get('vesg');
      System.debug('this.pesgval--->>>'+this.pesgval);
               if(pesgval==null || pesgval.length()==0){
                    PageReference secondPage1 = new PageReference('/apex/editLeadError');
                        secondPage1.setRedirect(true);
                  return secondPage1; 
                  
                 } else{//do something else, but the execution never comes here as the value of pesgval is always NULL}          
            return null;      
                }

 
The system.debug in the controller for this.pesgval always return null and the value is not fetched from the VF page.

All that I am struggling to achieve here is:
1. Creating a parameter (not query string) in the VF page
2. Assigning a value to it within javascript on pageload
3. Retrieving the parameter value from the controller
 
Appreciate if anyone can give their feedbacks on this or send some sample code for the same.
Thanks and regards,
Rasu
jwetzlerjwetzler
currentPage().getParameters() is only for query parameters.  You need to look at the assignTo attribute on apex: param
kgrasukgrasu

Hi, Thanks for your response.

But do you know how can we assign a value to the <Apex:param> using javascript. I am looking for the syntax for the same.

Thanks and regards,Rasu

Mark YoungMark Young
Hi Rasu,

I'm not 100% sure what you're after here, but following are some notes that might help.

  • The only parameters of a page that you can access in a controller are query string parameters.  The controller executes before the page renders, so any values set in javascript are only accessible after the next postback.
I think what you're attempting to do is load a page without params, then depending on conditions set a value which the controller which retrieve on the postback (and so can redirect to a different page etc).

The easiest way to do this is with a hidden input, so:
Code:
<apex:page>
    <apex:form>
        <apex:inputHidden value="{!vesg}" id="vesgInput" />

<apex:commandButton action="{!validate}" value="Validate" /> <script> document.getElementById('{!$Component.vesgInput}').value = 'Some value'; </script> </apex:form> </apex:page>

 
and controller would look like:
Code:
...

public String Vesg {get; set; }

public PageReference validate()
{
    if (this.Vesg == 'Some Value')
    {
        // Do action 1
    }
    else
    {

    }
}
...

 
Hope this helps,

Mark


Message Edited by Mark Young on 08-28-2008 03:38 PM
kgrasukgrasu

Hi Mark,

Thanks for your response. I think you got it right. But when I use the code as mentioned by you..the VF page throws the error: document.getElementById().. is null or not an object.

Any clues?

 

Thanks a lot for your time.

 

Mark YoungMark Young

Yes, this is pretty common.  Take a look at the page source and you'll see that {!$Component.Id} has returned an empty string.

The component has to be a sibling or direct parent or child of where you reference it.  Move the script to next to the component (and probably wrap it in a javascript function for easy of use) and that should solve the issue.

Mark



Message Edited by Mark Young on 08-28-2008 03:38 PM
Mark YoungMark Young
Actually my bad - the syntax is {!$Component.Id} - that'll teach me not to read my posts.
 
Mark
kgrasukgrasu

Hi Mark,

Thanks for your response. I got it to work after manipulating the position of the XML tag as mentioned.

Thanks a lot.

Regards