【问题标题】:Query the refreshed UpdatePanel with a if-clause in JavaScript在 JavaScript 中使用 if 子句查询刷新的 UpdatePanel
【发布时间】:2012-10-22 21:00:50
【问题描述】:

我有一个 UpdateProgress-Control 中的图像和一个 ModalPopupExtender。要在网站加载时打开扩展器,我使用以下 Javascript:

<script type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(showPopup);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(hidePopup);

    function showPopup(sender, args) {
        var ModalControl = '<%= ModalPopupExtender1.ClientID %>';
        $find(ModalControl).show();
    }

    function hidePopup(sender, args) {
        var ModalControl = '<%= ModalPopupExtender1.ClientID %>';
        $find(ModalControl).hide();
    }
</script> 

这工作正常,但扩展器现在打开每个请求。它仅应在特定的 UpdatePanel 被强制刷新 (UpdatePanelMain) 时打开。我怎样才能在 Javascript 中做到这一点:if(postbackelement == UpdatePanelMain) 之类的?

【问题讨论】:

    标签: javascript asp.net updatepanel


    【解决方案1】:

    要获取 UpdatePanels id 并找到您赢得的让运行的 ID,您可以使用 args.get_postBackElement().id 函数:

    function showPopup(sender, args) {
         // get the Post ID and compare it with the one you let run
         if(args.get_postBackElement().id == "<%=UpdatePanelMain.ClientID%>")
         {
           var ModalControl = '<%= ModalPopupExtender1.ClientID %>';
           $find(ModalControl).show();
         }
    }
    

    类似的问题: How to get Id update panel that initial a request in javascript

    【讨论】:

    • 对我不起作用。它说 args 不支持 get_postBackElement 函数或 id 为空。
    • @Jendrik 你可以试试var UpdPanelsIds = args.get_updatePanelsToUpdate(); 看看是否有你要找的id 吗?
    【解决方案2】:

    因此,我正在寻找这种解决方法。感谢 Aristos 的提示。

    var UpdPanelsIds = args.get_updatePanelsToUpdate();
    
    if (UpdPanelsIds[0] != "") {
        if (UpdPanelsIds[0] != "ctl00$cpMain$UpdatePanelMenue" || args.get_postBackElement().id == '<%= refreshButton.ClientID %>') {
            var ModalControl = '<%= ModalPopupExtender1.ClientID %>';
            $find(ModalControl).show();
        }
    }
    

    任何结论性的如何使这段代码“更好”一点?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-30
      • 2019-05-07
      • 2017-07-17
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多