【问题标题】:webforms : determines which control within updatepanel triggers the refreshwebforms : 确定 updatepanel 中的哪个控件触发刷新
【发布时间】:2016-08-26 13:41:45
【问题描述】:

我有一个这样的 aspx 页面:

<asp:UpdatePanel runat="server" UpdateMode="always">
    <ContentTemplate>
        <asp:DropDownList autopostback="true" runat="server" ID="DropDown1" OnSelectedIndexChanged="DropDown1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:DropDownList autopostback="true" runat="server" ID="DropDown2" OnSelectedIndexChanged="DropDown2_SelectedIndexChanged">
        </asp:DropDownList>
    </ContentTemplate>
</asp:UpdatePanel>

在 2 个下拉列表中的任何一个中的每个 selectionChanged 处,都会完成部分回发,并且我会刷新 updatePanel 内容。但是,我只想在 DropDown1 的 selectionChanged 触发刷新面板后执行一些 js 代码。

我怎样才能在我的页面中进入客户端,在 updatePanel 刷新之后的事件,更重要的是,触发刷新面板的控件的 id(在本例中为 DropDown1)。

我试过这个,但每次加载页面时都会调用它,即使是第一次,我也不知道哪个控件触发了 updatePanel 刷新:

<script>
  $(document).ready(function () 
  {       
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageLoaded)
    });

    function PageLoaded(sender, args) {
        // I have analyzed sender and args without any information to obtain the desired id "DropDown1"
    }

</script> 

【问题讨论】:

    标签: javascript c# jquery asp.net updatepanel


    【解决方案1】:

    您可以在 UpdatePanel 回发后使用sender._postBackSettings.sourceElement.id 获取源代码管理的 Id。请参阅下面的示例。

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdatePanelTest.aspx.cs" Inherits="TestApp.UpdatePanelTest" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="/Scripts/jquery-1.8.2.min.js"></script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="sm" ></asp:ScriptManager>
        <div>
        <asp:UpdatePanel runat="server" UpdateMode="always">
            <ContentTemplate>
                <asp:DropDownList autopostback="true" runat="server" ID="DropDown1" OnSelectedIndexChanged="DropDown1_SelectedIndexChanged">
                    <asp:ListItem Text="option 1" Value="1" />
                    <asp:ListItem Text="option 2" Value="2" />
                    <asp:ListItem Text="option 3" Value="3" />
                </asp:DropDownList>
                <asp:DropDownList autopostback="true" runat="server" ID="DropDown2" OnSelectedIndexChanged="DropDown2_SelectedIndexChanged">
                    <asp:ListItem Text="option A" Value="A" />
                    <asp:ListItem Text="option B" Value="B" />
                    <asp:ListItem Text="option C" Value="C" />
                </asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
        </div>
        <script type="text/javascript">
            $(document).ready(function () {
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
                    function (sender) {
                        if (sender._postBackSettings.sourceElement.id == 'DropDown1')
                            DoSomethingAmazing();
                    });
            });
    
            function DoSomethingAmazing() {
                alert('OMG something amazing just occurred!');
            }
        </script> 
        </form>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 2018-01-19
      相关资源
      最近更新 更多