如何获取启动回发的Updatepanel的ID

时间:2022-08-25 18:43:45

Hi I need to intercept server callback after udate panel async post back and determine which panel initiated the request. The code is pretty simple:

嗨我需要在udate面板异步回发后拦截服务器回调,并确定哪个面板发起了请求。代码非常简单:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InterceptUpdateCallback);

function InterceptUpdateCallback(sender, args)
{
    var updatedPanels = args.get_panelsUpdated();    
    for (idx = 0; idx < updatedPanels.length; idx++) {
        if (updatedPanels[idx].id == "myUpdatePanel") {            
            StartSmth();
            break;
        }
      }
}

And it works when UpdatePanel is not inside another UpdatePanel. But when it is inside another UpdatePanel updatedPanels[idx].id has parent Updatepanel id. So how can I get the id of UpdatePanel which initiated the request (the inner UpdatePanel)? Thanx

当UpdatePanel不在另一个UpdatePanel中时,它可以工作。但是当它在另一个UpdatePanel内时,updatedPanels [idx] .id具有父Updatepanel id。那么如何获取启动请求的UpdatePanel的ID(内部UpdatePanel)?感谢名单

3 个解决方案

#1


2  

Here you go:

干得好:

function InterceptUpdateCallback(sender, args) {
 if (sender._postBackSettings)
    alert(sender._postBackSettings.panelID);
 else
    alert('first load');    
}

Update:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void LinkButtons_Click(object sender, EventArgs e)
    {
        LabelMain.Text = LabelSub1.Text = LabelSub2.Text = LabelSub3.Text = string.Format("{0} Updated By {1}", DateTime.Now, ((Control)sender).ID);
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style type="text/css">
        body { font-family: Tahoma;}
        fieldset { padding: 15px; }
        fieldset a 
        {
            float: right;
            clear: none;
            display: block;
            margin: 10px;
        }
        fieldset span
        {
            display: block;
            margin-top: 20px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript">
        function pageLoaded(sender, args) {
            if (sender._postBackSettings) {
                var panelId = sender._postBackSettings.panelID.split('|')[0];
                if (panelId == sender._scriptManagerID) {
                    var updatedPanels = args.get_panelsUpdated();
                    var affectedPanels = "Affected Panels:\n";
                    for(var x=0;x<updatedPanels.length;x++)
                        affectedPanels+= updatedPanels[x].id + "\n";
                    alert("Request initiated by ScriptManager\n\nMight be an async trigger, or child of an update panel with children as triggers set to false.\n\n"+affectedPanels);
                }
                else
                    alert("Request initiated by: " + panelId);
            }
        }
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
    </script>

    <asp:LinkButton ID="UpdateMain" runat="server" Text="UpdateMain" OnClick="LinkButtons_Click"></asp:LinkButton>,
    <asp:LinkButton ID="UpdateSub1" runat="server" Text="UpdateSub1" OnClick="LinkButtons_Click"></asp:LinkButton>,
    <asp:LinkButton ID="UpdateSub2" runat="server" Text="UpdateSub2" OnClick="LinkButtons_Click"></asp:LinkButton>,
    <asp:LinkButton ID="UpdateSub3" runat="server" Text="UpdateSub3" OnClick="LinkButtons_Click"></asp:LinkButton>
    <br />
    <br />
    <asp:UpdatePanel ID="Main" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
        <ContentTemplate>
            <fieldset>
                <asp:LinkButton ID="LinkButton1" runat="server" Text="LinkButton1" OnClick="LinkButtons_Click"></asp:LinkButton>
                <legend>Main -  Update Mode:Conditional, Children As Triggers:False</legend>
                <asp:Label ID="LabelMain" runat="server" Text="LabelMain"></asp:Label>
                <asp:UpdatePanel ID="Sub1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
                    <ContentTemplate>
                        <fieldset>
                            <asp:LinkButton ID="LinkButton2" runat="server" Text="LinkButton2" OnClick="LinkButtons_Click"></asp:LinkButton>
                            <legend>Sub1 - Update Mode:Always, Children As Triggers:True</legend>
                            <asp:Label ID="LabelSub1" runat="server" Text="LabelSub1"></asp:Label>
                            <asp:UpdatePanel ID="Sub2" runat="server"  UpdateMode="Always" ChildrenAsTriggers="true">
                                <ContentTemplate>
                                    <fieldset>
                                        <asp:LinkButton ID="LinkButton3" runat="server" Text="LinkButton3" OnClick="LinkButtons_Click"></asp:LinkButton>
                                        <legend>Sub2 - Update Mode:Always, Children As Triggers:True</legend>
                                        <asp:Label ID="LabelSub2" runat="server" Text="LabelSub2"></asp:Label>
                                    </fieldset>
                                </ContentTemplate>
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="UpdateSub2" />
                                </Triggers>
                            </asp:UpdatePanel>                            
                        </fieldset>                        
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="UpdateSub1" />
                    </Triggers>
                </asp:UpdatePanel>
                <asp:UpdatePanel ID="Sub3" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
                    <ContentTemplate>

                        <fieldset>
                            <asp:LinkButton ID="LinkButton4" runat="server" Text="LinkButton4" OnClick="LinkButtons_Click"></asp:LinkButton>
                            <legend>Sub3 - Update Mode:Conditional, Children As Triggers:False</legend>
                            <asp:Label ID="LabelSub3" runat="server" Text="LabelSub3"></asp:Label>
                        </fieldset>                        
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="UpdateSub3" />
                    </Triggers>
                </asp:UpdatePanel>
            </fieldset>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="UpdateMain" />
        </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>

#2


0  

I will have a guess at this one.

我会猜到这个。

Does setting UpdateMode = Conditional on the outer (or both) UpdatePanels help? I think the problem is that you only get the "outermost" updated panel and if you do not set UpdateMode to Conditional the outer Panel is updated as well (even if you click something in the inner panel; see second reference).

在外部(或两个)UpdatePanels上设置UpdateMode = Conditional是否有帮助?我认为问题是你只获得了“最外层”的更新面板,如果你没有将UpdateMode设置为Conditional,外面面板也会更新(即使你点击内部面板中的内容;参见第二个参考)。

For reference see

供参考见

Note that, if I remove the property UpdateMode=Conditional for the UpdatePanel1 (parent), both the labels will get refreshed.

请注意,如果我为UpdatePanel1(父级)删除属性UpdateMode = Conditional,则两个标签都将刷新。

from ASP.NET 2.0 AJAX Extensions Update Panel - Nested Update Panel

来自ASP.NET 2.0 AJAX Extensions更新面板 - 嵌套更新面板

and

When set to Always, the UpdatePanel is updated on every postback raised from anywhere in the page, so from controls inside the panel, inside other panels or just on the page.

当设置为Always时,UpdatePanel会在页面中任何位置提升的每个回发时更新,因此可以从面板内部的控件,其他面板内部或页面上的控件进行更新。

from Remember to set UpdatePanel's UpdateMode to Conditional

从记住将UpdatePanel的UpdateMode设置为Conditional

#3


0  

Finally I came to solution: the problem was that I had trigger control (Button) for child UpdatePanel which actually was outside this Update panel and inside parent UpdatePanel (sorry I hadn't noticed that). If you put Button inside child UpdatePanel - everything works fine.

最后我找到了解决方案:问题是我有一个子UpdatePanel的触发控件(Button),它实际上在这个Update面板之外,在父UpdatePanel里面(抱歉我没注意到)。如果你将Button放在子UpdatePanel中 - 一切正常。

#1


2  

Here you go:

干得好:

function InterceptUpdateCallback(sender, args) {
 if (sender._postBackSettings)
    alert(sender._postBackSettings.panelID);
 else
    alert('first load');    
}

Update:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void LinkButtons_Click(object sender, EventArgs e)
    {
        LabelMain.Text = LabelSub1.Text = LabelSub2.Text = LabelSub3.Text = string.Format("{0} Updated By {1}", DateTime.Now, ((Control)sender).ID);
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style type="text/css">
        body { font-family: Tahoma;}
        fieldset { padding: 15px; }
        fieldset a 
        {
            float: right;
            clear: none;
            display: block;
            margin: 10px;
        }
        fieldset span
        {
            display: block;
            margin-top: 20px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript">
        function pageLoaded(sender, args) {
            if (sender._postBackSettings) {
                var panelId = sender._postBackSettings.panelID.split('|')[0];
                if (panelId == sender._scriptManagerID) {
                    var updatedPanels = args.get_panelsUpdated();
                    var affectedPanels = "Affected Panels:\n";
                    for(var x=0;x<updatedPanels.length;x++)
                        affectedPanels+= updatedPanels[x].id + "\n";
                    alert("Request initiated by ScriptManager\n\nMight be an async trigger, or child of an update panel with children as triggers set to false.\n\n"+affectedPanels);
                }
                else
                    alert("Request initiated by: " + panelId);
            }
        }
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
    </script>

    <asp:LinkButton ID="UpdateMain" runat="server" Text="UpdateMain" OnClick="LinkButtons_Click"></asp:LinkButton>,
    <asp:LinkButton ID="UpdateSub1" runat="server" Text="UpdateSub1" OnClick="LinkButtons_Click"></asp:LinkButton>,
    <asp:LinkButton ID="UpdateSub2" runat="server" Text="UpdateSub2" OnClick="LinkButtons_Click"></asp:LinkButton>,
    <asp:LinkButton ID="UpdateSub3" runat="server" Text="UpdateSub3" OnClick="LinkButtons_Click"></asp:LinkButton>
    <br />
    <br />
    <asp:UpdatePanel ID="Main" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
        <ContentTemplate>
            <fieldset>
                <asp:LinkButton ID="LinkButton1" runat="server" Text="LinkButton1" OnClick="LinkButtons_Click"></asp:LinkButton>
                <legend>Main -  Update Mode:Conditional, Children As Triggers:False</legend>
                <asp:Label ID="LabelMain" runat="server" Text="LabelMain"></asp:Label>
                <asp:UpdatePanel ID="Sub1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
                    <ContentTemplate>
                        <fieldset>
                            <asp:LinkButton ID="LinkButton2" runat="server" Text="LinkButton2" OnClick="LinkButtons_Click"></asp:LinkButton>
                            <legend>Sub1 - Update Mode:Always, Children As Triggers:True</legend>
                            <asp:Label ID="LabelSub1" runat="server" Text="LabelSub1"></asp:Label>
                            <asp:UpdatePanel ID="Sub2" runat="server"  UpdateMode="Always" ChildrenAsTriggers="true">
                                <ContentTemplate>
                                    <fieldset>
                                        <asp:LinkButton ID="LinkButton3" runat="server" Text="LinkButton3" OnClick="LinkButtons_Click"></asp:LinkButton>
                                        <legend>Sub2 - Update Mode:Always, Children As Triggers:True</legend>
                                        <asp:Label ID="LabelSub2" runat="server" Text="LabelSub2"></asp:Label>
                                    </fieldset>
                                </ContentTemplate>
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="UpdateSub2" />
                                </Triggers>
                            </asp:UpdatePanel>                            
                        </fieldset>                        
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="UpdateSub1" />
                    </Triggers>
                </asp:UpdatePanel>
                <asp:UpdatePanel ID="Sub3" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
                    <ContentTemplate>

                        <fieldset>
                            <asp:LinkButton ID="LinkButton4" runat="server" Text="LinkButton4" OnClick="LinkButtons_Click"></asp:LinkButton>
                            <legend>Sub3 - Update Mode:Conditional, Children As Triggers:False</legend>
                            <asp:Label ID="LabelSub3" runat="server" Text="LabelSub3"></asp:Label>
                        </fieldset>                        
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="UpdateSub3" />
                    </Triggers>
                </asp:UpdatePanel>
            </fieldset>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="UpdateMain" />
        </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>

#2


0  

I will have a guess at this one.

我会猜到这个。

Does setting UpdateMode = Conditional on the outer (or both) UpdatePanels help? I think the problem is that you only get the "outermost" updated panel and if you do not set UpdateMode to Conditional the outer Panel is updated as well (even if you click something in the inner panel; see second reference).

在外部(或两个)UpdatePanels上设置UpdateMode = Conditional是否有帮助?我认为问题是你只获得了“最外层”的更新面板,如果你没有将UpdateMode设置为Conditional,外面面板也会更新(即使你点击内部面板中的内容;参见第二个参考)。

For reference see

供参考见

Note that, if I remove the property UpdateMode=Conditional for the UpdatePanel1 (parent), both the labels will get refreshed.

请注意,如果我为UpdatePanel1(父级)删除属性UpdateMode = Conditional,则两个标签都将刷新。

from ASP.NET 2.0 AJAX Extensions Update Panel - Nested Update Panel

来自ASP.NET 2.0 AJAX Extensions更新面板 - 嵌套更新面板

and

When set to Always, the UpdatePanel is updated on every postback raised from anywhere in the page, so from controls inside the panel, inside other panels or just on the page.

当设置为Always时,UpdatePanel会在页面中任何位置提升的每个回发时更新,因此可以从面板内部的控件,其他面板内部或页面上的控件进行更新。

from Remember to set UpdatePanel's UpdateMode to Conditional

从记住将UpdatePanel的UpdateMode设置为Conditional

#3


0  

Finally I came to solution: the problem was that I had trigger control (Button) for child UpdatePanel which actually was outside this Update panel and inside parent UpdatePanel (sorry I hadn't noticed that). If you put Button inside child UpdatePanel - everything works fine.

最后我找到了解决方案:问题是我有一个子UpdatePanel的触发控件(Button),它实际上在这个Update面板之外,在父UpdatePanel里面(抱歉我没注意到)。如果你将Button放在子UpdatePanel中 - 一切正常。