0

VF Page:

<apex:page controller="Ctrl_Event_Registration">
<apex:form >
<apex:pageBlock title="Event Registration">
<apex:pageBlockButtons >
<apex:commandButton action="{!onclick}" value="Mark Attended" reRender="out"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Select the Event Name">    
<apex:selectList value="{!events}" multiselect="false">
<apex:selectOptions value="{!selectEvent}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSection>  
<apex:pageBlockSection title="Select the Contact Name">    
<apex:selectList value="{!contacts}" multiselect="false">
<apex:selectOptions value="{!selectContact}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSection>            
</apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public class Ctrl_Event_Registration {

public String contacts{get;set;}
public String events{get;set;}

public Event_Registration__c eve{get;set;}
List<SelectOption> eventOptions = new List<SelectOption>();
 List<SelectOption> contactOptions = new List<SelectOption>();

public Ctrl_Event_Registration ()
{

}
public PageReference onclick()
{
    try{
    system.debug('eveeeeeeeeeeee...........' + eve );
        if(eve != null)
        {
            for(Event_Registration__c register : [SELECT id,Event__c,Name ,Attended_Event__c,Contact__c 
                                                    FROM Event_Registration__c])
                                                    {
                                                        register.Attended_Event__c = true;
                                                    }
            update eve;
        }
    }catch(DMLException ex)
    {
        ApexPages.addMessages(ex);  
    }
    return null;
}

public List<SelectOption> getSelectEvent() {

    eventOptions.add(new SelectOption('','--None--'));
    for(Event__c eve : [SELECT id,name From Event__c WHERE Event_Start_Date__c > Today])   
    {
     eventOptions.add(new SelectOption(eve.id,eve.Name));   
    }  

    return eventOptions;
}

public List<SelectOption> getSelectContact(){

    contactOptions .add(new SelectOption('','-None-'));       
    for(Event_Registration__c  ev : [SELECT id,Contact__r.Name from Event_Registration__c where Event__r.Event_Start_Date__c > Today and Attended_Event__c =false])
    {
        contactOptions .add(new selectOption(ev.id,ev.Contact__r.Name));
    }
    return contactOptions ;
}

public List<SelectOption> options {get; set;}
    public string Choice {get; set;}
    public string choiceName {get{
        for (SelectOption so : Options){
            if (so.getValue() == choice){
                return so.getLabel();
            }
        }
        return null;    
    }}
    public String getChoice() {
        return Choice;
    }  
public String getEvents()
{
    return events;
    }

public void setEvents(String events)
{
    this.events = events;
}
}

I want the id of the selected item from the picklist and query it in onclick function.

1

2 Answers 2

1

You can retrieve the selected values from the Selectlist in your controller as you already have a setter and getter for the strings events and contacts.

Basically calling the onclick method should show you the record id values for the events and contacts selected:

public PageReference onclick() {
    system.debug('events...........' + events);
    system.debug('contacts.........' + contacts);
    return null;
}
0

You can use an apex ActionFunction to accomplish this. Call the name of the action function for the Select List onChange method. Then use the action function to reference the desired method in your controller.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .