0

I had a quiz program in apex which displays the next question value when the 'Next' button is clicked and it's working fine. But when I select a value in the radio button which I had used for choices for questions, the next button does not refresh the page. I have no idea why its not working!?

VisualForce page:

<apex:page Controller="Quiz_QuestionDisplay_Controller" >
  <apex:form >
    <apex:actionRegion >
      <apex:pageBlock >   
        <apex:outputPanel layout="block" id="panel"  >
          <apex:pageBlockSection columns="1" id="pgblksec" >
            <apex:inputField value="{!current_Question.Question_No__c}" id="t1" ></apex:inputField>
            <apex:inputField value="{!current_Question.Question_Detail__c}" id="t2" required="true" ></apex:inputField>
            <apex:selectRadio value="{!current_Question.user_Selected_Ans__c}" >
              <apex:selectOptions value="{!choices}"/>
              <apex:actionSupport event="onchange"    action="{!checkSelectedValue}" reRender="none"/>
            </apex:selectRadio><p/>
            <apex:outputText value="{!index}" />
            <apex:outputText value="{!size}"/>
            <apex:commandButton value="next" action="{!next_Click}" rerender="panel"/>
            <apex:commandButton value="prev" action="{!prev_Click}" rerender="panel"/>
            <apex:commandButton value="submit" action="{!user_Submit}" />
          </apex:pageBlockSection>
        </apex:outputPanel>
      </apex:pageBlock>
    </apex:actionRegion>
  </apex:form>
</apex:page>

Controller:

public class Quiz_QuestionDisplay_Controller {
    public Integer index{get;set;}
    //Integer chosen_Value {get;set;}
    Map<Integer,Decimal> answers;

    public  List<Question__c> questions{get;set;}
    public List<SelectOption> choices=new List<SelectOption>();
    public Question__c current_Question;
    public Integer size{get;set;}


    public Quiz_QuestionDisplay_Controller()
    {
        index=0;
        count=0;
        //user_Selected_Ans=-1;
        answers=new Map<Integer,Decimal>();
        questions= [SELECT Question_No__c,user_Selected_Ans__c,Question_Detail__c,choice1__c,choice2__c,choice3__c,choice4__c,ans__c FROM Question__c LIMIT 1000];
        size=questions.size();
        current_Question=questions.get(index);
    }

    public List<SelectOption> getChoices()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('choice1',current_Question.choice1__c));
        options.add(new SelectOption('choice2',current_Question.choice2__c));
        options.add(new SelectOption('choice3',current_Question.choice3__c));
        options.add(new SelectOption('choice4',current_Question.choice4__c));
        return options;
    }

    public void checkSelectedValue()
    {
        Question__c ob=questions.get(index);

        System.debug('Selected value is: ' + ob.user_Selected_Ans__c); 
        System.debug('Selected value is: ' + current_Question.user_Selected_Ans__c); 
    }

    public Question__c getCurrent_Question()
    {
        return  current_Question;        
    }
    public void setQuestion(Question__c value)
    {
        current_Question=value;
    }
    public void next_Click()
    {
        //   PageReference pageRef = ApexPages.currentPage();
        Integer org=index;
        index++;
        if(index==size)
            index=0;
        answers.put(org,questions.get(org).user_Selected_Ans__c);
        current_Question=questions.get(index);
        //return pageRef;
    }
    public void prev_click()
    {
        Integer org=index;
        // PageReference pageRef = ApexPages.currentPage();
        //error to be handled
        index--;
        if(index==-1)
            index=size-1;

        answers.put(org,questions.get(org).user_Selected_Ans__c);
        current_Question=questions.get(index);

        // return pageRef;
    }
    public void user_Submit()
    {

    }
}
5
  • please add you code. are you using rerender ?
    – Ratan Paul
    Dec 14, 2015 at 10:44
  • hi ratan . I had added my code
    – Shiva Rv
    Dec 14, 2015 at 11:05
  • No ratan, its not working
    – Shiva Rv
    Dec 14, 2015 at 11:19
  • i believe its for faster access ... i dono the exact benefit of it ;) by the way @Ratan , the code works when i didnt click the radio button,if i click that button the next or previous button doesnt work! thats the exact problem
    – Shiva Rv
    Dec 14, 2015 at 11:40
  • Try to remove actionRegion and try without it, or wrap only the selectOptions tag with it . And remove unnecessary <p/> tag. Dec 14, 2015 at 12:17

1 Answer 1

0

The is that the radio button cannot write back the selected value into the controller # property.

Your radio button is trying to write back to

current_Question.user_Selected_Ans__c

Which seems to be a number (Decimal) by looking at the code. However, the property must be a string: the selectOption class only takes a String 'value' argument .

I have fixed this by creating a String backing property. If you want to, you can store this in the current_Question property by parsing it into a decimal.

On top of that, I took the liberty of fixing other issues in the controller. Namely:

  • Some properties should be read only, as they have to do with the internal state of the controller, so I've marked the setters as private
  • The Current_Question property getters and setters didn't do much so I turned it into an auto-declared property
  • I have simplified the index logic on the 'next' and 'prev' callbacks
  • The questions are not sorted by question number, so I have added an ORDER BY clause

The final controller looks like this:

public class Quiz_QuestionDisplay_Controller {

    public Integer index {get; private set;}
    public String Chosen_Value {get;set;}
    Map<Integer,Decimal> answers;
    public List<Question__c> questions {get; private set;}
    public List<SelectOption> choices=new List<SelectOption>();
    public Question__c current_Question { get; private set;}
    public Integer size {get;private set;}


    public Quiz_QuestionDisplay_Controller()
    {
        index=0;
        answers=new Map<Integer,Decimal>();
        questions= [SELECT Question_No__c,user_Selected_Ans__c,Question_Detail__c,choice1__c,choice2__c,choice3__c,choice4__c,ans__c FROM Question__c ORDER BY Question_No__c LIMIT 1000];
        size=questions.size();
        current_Question=questions.get(index);
    }

    public List<SelectOption> getChoices()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('1',current_Question.choice1__c));
        options.add(new SelectOption('2',current_Question.choice2__c));
        options.add(new SelectOption('3',current_Question.choice3__c));
        options.add(new SelectOption('4',current_Question.choice4__c));
        return options;
    }

    public void checkSelectedValue()
    {
        Question__c ob=questions.get(index);

        System.debug('Selected value is: ' + ob.user_Selected_Ans__c); 
        System.debug('Selected value is: ' + current_Question.user_Selected_Ans__c); 
    }

    public void next_Click()
    {
        answers.put(index,questions.get(index).user_Selected_Ans__c);
        if(++index==size) index=0;
        current_Question=questions.get(index);
        Chosen_Value = null;
    }
    public void prev_click()
    {
        answers.put(index,questions.get(index).user_Selected_Ans__c);
        if(--index==-1) index=size-1;
        current_Question=questions.get(index);
        Chosen_Value = null;
    }
    public void user_Submit()
    {

    }
}

If you want to persist the value of the selected check box when the user clicks forward and back, you will have to update the Question object with the selected value, and set the Chosen_Value property every time the user clicks 'next' or 'prev'.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.