function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mayank RanaMayank Rana 

Passing selected picklist value to apex controller using apex:actionSupport

Hi All,

I want the selected picklist value along with there domain(using <apex:param>) displayed in the below <apex:pageBlockSection> Title. I have two  picklist ,As I select the value from another picklist the previous count value and domain both must be updated. But In my case count value for the first picklist value displayed null, but the last picklist slected value in displayed fine. I didnt understand its behavior. I am sending VisualForce Page and Controller.. Please give the solution
soon please..
Thanks in advance.

VisualForce Page:
<apex:page controller="Custom_Picklist" tabStyle="Contact">
    <apex:form >
        <apex:pageBlock >
            <h1>Salesforce</h1>
            <apex:selectList size="1" value="{!count}">
                                <apex:selectOptions value="{!numberOfQuestions }"/>
                                    <apex:actionSupport event="onchange" action="{!showQuestions}" reRender="abcd">
                                        <apex:param name="Domain" value="Salesforce" assignTo="{!Domain}" />
                                    </apex:actionSupport>
                             </apex:selectList>
                             
              <h1>PHP</h1>
            <apex:selectList size="1" value="{!count}">
                                <apex:selectOptions value="{!numberOfQuestions }"/>
                                    <apex:actionSupport event="onchange" action="{!showQuestions}" reRender="abcd">
                                        <apex:param name="Domain" value="PHP" assignTo="{!Domain}" />
                                    </apex:actionSupport>
                             </apex:selectList>
        <apex:pageBlockSection title="{!message}" id="abcd">
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class Custom_Picklist
{
    public String count { get;set;}
    public String Domain { get; set; }
    public String message {get;set;}
    
    public List<SelectOption> numberOfQuestions {get;set;}
    public String selected {get;set;}
    public Custom_Picklist()
    {
        message='';
        this.getnumberOfQuestions();     
    }
    
    public List<SelectOption> getnumberOfQuestions()
    {
        numberOfQuestions = new List<SelectOption>();
        numberOfQuestions.add(new SelectOption('','--None--'));
        numberOfQuestions.add(new SelectOption('5','5'));
        numberOfQuestions.add(new SelectOption('10','10'));
        numberOfQuestions.add(new SelectOption('20','20'));
        numberOfQuestions.add(new SelectOption('30','30'));
        
        return  numberOfQuestions;
    }
    
    public void showQuestions()
    {
        message=Domain+'_'+count;
    }
}
Best Answer chosen by Mayank Rana
Mayank RanaMayank Rana
Thanks Satish Prajapat, You cleared my doubt.

All Answers

Satish PrajapatSatish Prajapat
Hello Mayank Rana,
You were using the same variable name in both list which change the count variable value.
Instead of using single variable you should use one more variable.
I am adding corrected VF page and Apex class.
//Apex Class
public class Custom_Picklist
{
    public String count1 { get;set;}
    public String count2 { get;set;}
    public String Domain { get; set; }
    public String message {get;set;}
    public List<SelectOption> numberOfQuestions {get;set;}
    public String selected {get;set;}
    public Custom_Picklist()
    {
        message='';
        
        this.getnumberOfQuestions();
    }
    
    public List<SelectOption> getnumberOfQuestions()
    {
        numberOfQuestions = new List<SelectOption>();
        numberOfQuestions.add(new SelectOption('','--None--'));
        numberOfQuestions.add(new SelectOption('5','5'));
        numberOfQuestions.add(new SelectOption('10','10'));
        numberOfQuestions.add(new SelectOption('20','20'));
        numberOfQuestions.add(new SelectOption('30','30'));
        return  numberOfQuestions;
    }
    
    public void showQuestions()
    {
        if(Domain == 'Salesforce')
        {
            message = Domain + '_' + count1;
            Domain =null;
        }
        else if(Domain == 'PHP')
        {
            message = Domain + '_' + count2;
            Domain =null;
        }
        
    }
}

///VF Page
<apex:page controller="Custom_Picklist" tabStyle="Contact">
    <apex:form >
        <apex:pageBlock >
            <h1>Salesforce</h1>
            <apex:selectList size="1" value="{!count1}">
                <apex:selectOptions value="{!numberOfQuestions }"/>
                <apex:actionSupport event="onchange" action="{!showQuestions}" reRender="abcd">
                    <apex:param value="Salesforce" assignTo="{!Domain}" />
                </apex:actionSupport>
            </apex:selectList>
            
            <h1>PHP</h1>
            <apex:selectList size="1" value="{!count2}">
                <apex:selectOptions value="{!numberOfQuestions }"/>
                <apex:actionSupport event="onchange" action="{!showQuestions}" reRender="abcd">
                    <apex:param value="PHP" assignTo="{!Domain}" />
                </apex:actionSupport>
            </apex:selectList>
            <apex:pageBlockSection title="{!message}" id="abcd">
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

If this suggestion will help you, then mark this as a best answer!!!
otherwise tell us your doubt.
Mayank RanaMayank Rana
Thanks Satish Prajapat, You cleared my doubt.
This was selected as the best answer
Satish PrajapatSatish Prajapat
Hello Mayank Rana please select my response is best answer.