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
onetwokathreeonetwokathree 

problem with actionsupport onchange event in selectlist

We have two dropdown lists binded from SOQL query in apex programming, 

on change event we want related value in other dropdown lis. the code is given below

 

Onchange event of course list we have called method  {!getsublist} ,which get courses1 value and pass to the query. which is not returning subjects related to the courses

 

 

VF Page Code:

      <apex:actionRegion >
            <apex:panelGrid columns="2">                   
                   <apex:outputlabel value="Select Course" for="values" />
                   <apex:selectList value="{!courses}" size="1" >
                    <apex:actionSupport event="onchange" action="{!getsublist}" rerender="subjt"/>                                   
                     <apex:selectOptions value="{!SelectCourseoptions}"/>
                     </apex:selectList>
            </apex:panelGrid> 

              </apex:actionRegion>
                    <apex:pageblockSection id="dependentSubjectType">
                    <apex:panelGrid columns="2">
                            <apex:outputLabel value="Select Subject" />
                            <apex:selectList disabled="false" id="subjt" value="{!subjects}" size="1" >
                                <apex:selectOptions value="{!SelectSubjects}"/>
                            </apex:selectList>
                    </apex:panelGrid>
                    </apex:pageBlockSection>

 

 

Apex Code :

 

public class AddQuestionsextensions
{
    //The code creates an sObject with the structure of the Add_Question__c object, and gives that object the name of 'addquestion'. This object will only be accessible to methods inside this class, so it is defined as private
    private final Add_Question__c addquestion;
    course__c c;

     public string courses1;
     public string subjects;

       public String courses
        {
            get {return courses1;}
            set {courses1= value;}
        }
          


       public String getsubjects()
        {
            return subjects;
        }
          
       public  void setsubjects(String subjects)
        {
            this.subjects= subjects;
        }       

    //The 'addquestion' variable is now tied to the same record as the standard controller, allowing the controller extension to operate in synch with the standard controller.   
    public AddQuestionsextensions(ApexPages.StandardController addquestionController)
    {
    this.addquestion= (Add_Question__c)addquestionController.getRecord();
    }
    public List<selectOption> getSelectCourseoptions()
    {

            List<selectOption> SelectCourse = new List<selectOption>();

              List<Course__c> courselist = new List<Course__c>();
              courselist = [Select  id, Main_course_name__c FROM Course__c ];
              SelectCourse.add(new SelectOption('0','Select Course'));
              for (Integer j=0;j<courselist.size();j++)
              {
                  SelectCourse.add(new SelectOption(courselist[j].id,courselist[j].Main_course_name__c));
                 
              }      
            return SelectCourse ;

    }  

        public List<selectOption> getSelectSubjects()
        {

          //c.id=add_question__c.instruction__c;
          List<selectOption> SelectSubject =  new List<selectOption>();
          List<Subject__c> subjectlist= new List<Subject__c>();
         
          subjectlist= [Select Subject_Name__c FROM Subject__c s where Parent_Course__r.Main_course_name__c =:courses1];


          SelectSubject.add(new SelectOption('0','Select Subject'));
          for (Integer j=0;j<subjectlist.size();j++)
          {          
            SelectSubject.add(new SelectOption (subjectlist[j].Id,subjectlist[j].Subject_Name__c));
                       
          }           
        return SelectSubject;
        }


      public void getsublist()
      {
        getSelectSubjects(); 
      }
}

 

 

Rajesh ShahRajesh Shah

Try the following:

 

List<selectOption> SelectSubject {get; set;} public void getsublist() { //c.id=add_question__c.instruction__c; SelectSubject = new List<selectOption>(); List<Subject__c> subjectlist= new List<Subject__c>(); subjectlist= [Select Subject_Name__c FROM Subject__c s where Parent_Course__r.Main_course_name__c =:courses1]; SelectSubject.add(new SelectOption('0','Select Subject')); for (Integer j=0;j<subjectlist.size();j++) { SelectSubject.add(new SelectOption (subjectlist[j].Id,subjectlist[j].Subject_Name__c)); } }