apex:commandButton in visualforce 组件不调用控制器方法



我在visualforce组件中有一个commandButton。 预期的行为是调用控制器方法。 页面将刷新,但不会调用在命令按钮{!performUnlinkContact}中注册的方法。 奇怪的是,如果我将按钮放在基本视觉力页面上,它会按预期调用控制器方法 - 但是在组件中时,它不会。

这是我的组件:

<apex:component >
    <apex:attribute name="rk" description="RK Base Contact Data"  type="Object" required="true"/>
    <div class="unlinkBox">
        <div class="unlinkHeader">
            <div class="unlinkHeaderLeft">Wrong Contact?</div>
            <div class="unlinkHeaderRight"><a href=""  onclick="return hideUnlinkPanel()"><apex:image style="cursor: pointer;" value="{!$Resource.x}" alt="Close Panel" /></a></div>
        </div>
        <div class="unlinkBody">
            <div class="unlinkBodyLeft">
                Click Yes if the Contact displayed is incorrect.<br/><br/>You will be given the opportunity to link to a different Contact.
            </div>
            <div class="unlinkBodyRight">
                <apex:outputpanel id="callMethod">
                    <apex:commandbutton value="Yes, this is the wrong contact" action="{!performUnlinkContact}" rerender="" status="myDisplayStatus" />&nbsp;&nbsp;&nbsp;&nbsp;<a onclick="return hideUnlinkPanel()" style="color:blue;cursor:pointer;text-decoration:underline">No</a>
                </apex:outputpanel>
                <apex:actionStatus id="myDisplayStatus"  startText="(performing Contact Unlink...)"  stopText=""/>
            </div>
        </div>
    </div>
</apex:component>

这是控制器扩展中的方法:

public PageReference performUnlinkContact() {
     System.debug('==================================!!performUnlink');
    if (this.thisLead.rkContactId__c != 0) {
        this.thisLead.rkContactId__c = 0;
        update this.thisLead;
    }
    return null;
}

确定我一定做错了什么,因为我是 salesforce 的新手并且仍在学习,但是当我搜索如何解决此问题时,我找不到任何相关问题。 提前感谢您的帮助。

感谢您的回复 - 为了解决 我最终在基本页面中添加了一个 actionFunction,然后只是通过按钮的 onclick 事件在组件中调用此函数:

基本页面中的代码:

    <apex:actionFunction name="doUnlink" action="{!performUnlinkContact}" rerender="refresh" status="myStatus"/>

在组件中调用函数的代码:

    <input type="button" value="Yes, this is the wrong contact" onclick="doUnlink();" />

到目前为止,这种方法似乎效果很好。

您还可以使用 ApexPages.Action 类型的 apex:attribute 将 performUnlinkContact() 的引用传递给组件,然后当您的命令按钮使用此引用时,它将调用页面控制器中定义的方法。

<apex:component>
    <apex:attribute name="performUnlinkContact" type="ApexPages.Action" description="Passing method from controller"/>
    <!-- your stuff here -->
</apex:component>

然后,当您使用组件时,您必须像这样填充该属性:

<c:MyComponent performUnlinkContact="{!performUnlinkContact}"

我认为rerender=""不是执行假 reRender 命令的方法。我会和rerender="none"一起尝试.

最新更新