使用带有PopupControlExtender的onCheckedChanged并希望在取消选中时阻止弹出窗口

时间:2012-07-10 08:11:43

标签: c# asp.net visual-studio asp.net-ajax ajaxcontroltoolkit

我仍然相当新,并试图找到答案,所以希望我不要重复这个。

我使用的是ASP.NET,并且有一个复选框控件,当它被更改时会弹出一个弹出框,使用onCheckedChanged方法。这个弹出框中有一些信息和一个“关闭”按钮,可以成功关闭弹出窗口。

我想要的是在取消选中复选框时阻止显示弹出窗口。我目前有onCheckedChanged调用一个代码隐藏方法,如果没有选中控件,取消扩展程序调用,但弹出窗口会在关闭之前快速显示。我怎么能阻止它?

这是合适的页面代码:

        <div class="row" id="divDDMandate" runat="server">

        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <asp:CheckBox ID="chkDDMandate" Width="20px" Visible="true" runat="server" AutoPostBack="true"
                 OnCheckedChanged="clientchkDDMandateChanged(this);" on  />        
                <asp:Literal ID="ltlDDMandate" runat="server">Direct Debit Mandate (by post)</asp:Literal>

            <asp:PopupControlExtender ID="chkDDMandate_PopupControlExtender" runat="server" 
                DynamicServicePath="" Enabled="true" PopupControlID="PanelDDMandateDownload"
                TargetControlID="chkDDMandate"
                Position="Bottom" OffsetX="-20" OffsetY="10" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

...这是我的代码隐藏方法:

        protected void chkDDMandateChanged(object sender, EventArgs e)
    {
        //Cancel the panel if unchecking
        if ((!chkDDMandate.Checked) && chkDDMandate_PopupControlExtender.Enabled)
        {
            chkDDMandate_PopupControlExtender.Cancel();
        }
    }

如果有任何帮助,我将不胜感激。

干杯

2 个答案:

答案 0 :(得分:0)

从chkDDMandate复选框中删除AutoPostBack="true",并在ScriptManager控件之后添加以下脚本:

<script type="text/javascript">
    function pageLoad() {
        var extender = $find("<%= chkDDMandate_PopupControlExtender.ClientID %>");
        extender.remove_showing(onPopupShowing);
        extender.add_showing(onPopupShowing);
    }

    function onPopupShowing(sender, args) {
        var checkBoxChecked = $get("<%= chkDDMandate.ClientID %>").checked;
        args.set_cancel(!checkBoxChecked);
    }
</script>

答案 1 :(得分:0)

在Yuriy向我提供事件处理程序之后,我不得不求助于使用隐藏字段来跟踪弹出窗口和复选框的可见性。

这是因为我不想在删除刻度线时出现弹出窗口以及onClick方法使用设置复选框控件的设置这一事实,而onShowing方法使用当前可见的设置控制。我不得不使用隐藏的字段来保持可见性设置,并在我想要的时候更新它们。

我很惊讶popup扩展器的_visible属性总是设置为'false',所以我也不能使用它。

这可能有点像黑客,但这是我目前的任何感兴趣的javascript代码:

    <script type="text/javascript">

     function pageLoad() {
     // Attach an event handler for over-riding the showing Popup.
         var extender = $find("PopupControlExtenderBehaviorID");
         extender.remove_showing(onPopupShowing);
         extender.add_showing(onPopupShowing);

     // Initialise the hidden fields based on the page status after refresh.
         var hfPopup = $get("ctl00_body_PopupVisibleID");
         var hfCheckbox = $get("ctl00_body_CheckboxChecked");

     // Popup will always be hidden on page refresh
         hfPopup.value = "Hidden";
         hfCheckbox.value = $get("ctl00_body_chkDDMandate").checked;
     }  

     function onPopupShowing(sender, args) {
        // This function will over-ride the Popup showing if applicable.
         var popupVisible = $get("ctl00_body_PopupVisibleID");
         var checkboxChecked = $get("ctl00_body_CheckboxChecked");

        // If Popup hidden and 'tick' being taken out of the Checkbox, don't show the Popup.
         if (popupVisible.value == "Hidden" && checkboxChecked.value == "true") {
             args.set_cancel(true);
         }
         else if (popupVisible.value == "Hidden") {
            popupVisible.value = "Visible";
            }
            else {popupVisible.value = "Hidden";}

     }

    function OnClientClickCheck(o) {
        // This function will set the Hidden field value of Checkbox.
        // This is because when the OnClick method reads the control checkbox value it uses the value it's 
        // being set to; whereas, the onPopupShowing method uses the value it is currently displaying!
        var pce = $find('PopupControlExtenderBehaviorID');
        var checkboxChecked = $get("ctl00_body_CheckboxChecked");
        var isChecked = o.checked;

        if (isChecked) {
        // isChecked is what it is being changed to...
            checkboxChecked.value = "false";
        }
        else {
            checkboxChecked.value = "true";
        }
        pce.showPopup();
    }
</script>

感谢您的帮助。

相关问题