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
s-Forces-Force 

problem in command button

Hello Everyone,

 

I am facing some problem in command button. pls help me if anyone knw.

 

I want when i click on command button it opens the new pageblocksection with some of values. i am using javascript for the same with onclick event, it shows the pageblocksection but for some time and then refresh the page. if anyone knw pls tell me the error.

 

or if their any other solution for this??????

 

code that am using.... pls check.

<apex:page controller="TimeTable" sidebar="false">
 <script> 
        
    function Function()
    {   
         document.getElementById('idSpan').style.display = 'block';
                
    }
 </script>
<apex:form >
<apex:pageBlock title="Time Table">
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!timetable.Date__c}"/>
<apex:inputField value="{!timetable.Class__c}" />
<center><apex:commandButton value="Create Time Table" onclick="Function();"/></center>
</apex:pageBlockSection>
<span id="idSpan" style = "display:none">
<apex:pageBlock >
<apex:pageBlockSection title="Time Table" columns="1" id="Details">
<table>
<tr><th width="100">Period</th><th width="100">MON</th><th width="100">TUE</th><th width="100">WED</th><th width="100">THU</th>
<th width="100">FRI</th><th width="100">SAT</th></tr>
<tr><td width="100">Period-1<br/>8:00-9:00 AM</td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td>
<td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td></tr>
</table>
</apex:pageBlockSection>
</apex:pageBlock>
</span>
</apex:pageBlock>
</apex:form>
</apex:page>

 Thanks In Advance.

 

Best Answer chosen by Admin (Salesforce Developers) 
s-Forces-Force

Thanks Dear for reply......

 

Its working fine with after doing some of changes in given code...... Thanks for suggestion.....:)

 

 

 

All Answers

rmehrmeh

Hi Maahi,

 

Use the rerender attribute in the commandButton.

(For example : <apex:commandButton value="Save" onclick="javascript&colon; xyz()" rerender="specify the id of the part you wish to display") />

This will prevent refreshing of the page.

 

 

Hope this works for you.

s-Forces-Force

Thanks for reply...., i tried that also.. but not working properly...... i'll try ur example...... but if you have somthing else then pls let me knw.... or if u have code then pls post that.......

b-Forceb-Force

command button will refresh the page,

onclick you are just rendering some other section,

Then why you are not using simple HTML button , and call Javascript  function

It will not refresh your page

 

Hope will help you

 

Thanks,

Bala 

prageethprageeth

Hello Maahi;

Try adding "return false;" at the end of your buttons onclick event as below.

 

 

<apex:commandButton value="Create Time Table" onclick="Function();return false;"/>

 

 

rmehrmeh

Hi Maahi,

 

I have tested the same code in my org.

Do the changes marked in red as below and this will definitely work.

 

 

<apex:page controller="TimeTable" sidebar="false">
<script>

function Function()
{
document.getElementById('idSpan').style.display = 'block';
return false; // add here return false

}
</script>
<apex:form >
<apex:outputPanel id="test123">
<apex:pageBlock title="Time Table">
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!timetable.Date__c}"/>
<apex:inputField value="{!timetable.Class__c}" />
<center><apex:commandButton value="Create Time Table" onclick="javascript&colon; return Function();" rerender="opDummy"/></center>
</apex:pageBlockSection>
<span id="idSpan" style = "display:none">
<apex:pageBlock >
<apex:pageBlockSection title="Time Table" columns="1" id="Details">
<table>
<tr><th width="100">Period</th><th width="100">MON</th><th width="100">TUE</th><th width="100">WED</th><th width="100">THU</th>
<th width="100">FRI</th><th width="100">SAT</th></tr>
<tr><td width="100">Period-1<br/>8:00-9:00 AM</td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td>
<td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td><td width="100"><apex:inputField value="{!timetable.Teacher__c}"/></td></tr>
</table>
</apex:pageBlockSection>
</apex:pageBlock>
</span>
</apex:pageBlock>
</apex:outputPanel>
<apex:outputPanel id="opDummy"/>
</apex:form>
</apex:page>

 Please check.

 

s-Forces-Force

Thanks Dear for reply......

 

Its working fine with after doing some of changes in given code...... Thanks for suggestion.....:)

 

 

 

This was selected as the best answer