jquery Hide/Show 页面部分回发后无法正常工作

jquery Hide/Show not working after partial postback of the page

我有一个带有一些级联下拉菜单和一个提交按钮的网络表单。单击提交按钮后,我从 database.The 获取数据 整个网络表单都在更新面板中,包括按钮。

问题是,我正在尝试在网络表单中显示更新进度。我能够做到,但无论如何都无法隐藏它。因此,我尝试将更新进度放在 a 中并尝试使用 jquery.

隐藏 div

我在第一个 load.But 期间隐藏 div 有点成功,如果我在下拉列表中进行了一些选择并再次提交数据,则更新进度不会显示。我的意思是,最初在第一次加载页面和单击事件期间,更新进度显示并隐藏 out.But 在级联下拉列表中进行一些选择后,页面将部分回发,更新进度不是第二次点击或其他再次出现。

除了页面的新鲜加载之外,在其他情况下部分回发happens.So更新进度在部分回发情况下不显示。

注意:- 网络表单是母版页的内容页...

我在这里发布 jquery 和代码。请看看并纠正我做错的地方,请提出正确的建议。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>

    <script type="text/javascript">
        $(function() {
            setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
            $('#btnSearch').click(function() {
                $('#Progress').show();
                setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
            })
        })
    </script>
<asp:UpdatePanel ID="updatepanel1" runat="server">
        <ContentTemplate>
            <h2 style="text-decoration: underline; font-family: 'Courier New'; text-align: center;"
                align="center">
                Plantation KML File</h2>
            <asp:Panel ID="pnlloc" runat="server" BorderColor="#365672" BorderStyle="Double"
                Height="150px" HorizontalAlign="Center" BackColor="LightSteelBlue" Width="85%">
                <br />
                <table align="center" class="style1" style="height: 116px; width: 94%">
                    <tr>
                        <td class="style68">
                            <asp:Label ID="lblCircle" runat="server" Text="Circle"></asp:Label>
                        </td>
                        <td class="style115">
                            &nbsp;
                        </td>
                        <td class="style23">
                            <asp:DropDownList ID="ddCircle" runat="server" Width="150px">
                            </asp:DropDownList>
                            <asp:CascadingDropDown ID="ddCircle_CascadingDropDown" runat="server" BehaviorID="circleajax"
                                Category="Circle" Enabled="True" LoadingText="Loading Circles" PromptText="Select a Circle"
                                ServiceMethod="GetWLTRSFCircles" ServicePath="~/Common/WebService/GetMasters.asmx"
                                TargetControlID="ddCircle">
                            </asp:CascadingDropDown>
                        </td>
                        <td class="style23">
                            &nbsp;
                        </td>
                        <td class="style67">
                            <asp:Label ID="lblDivision" runat="server" Text="Division"></asp:Label>
                        </td>
                        <td class="style116">
                            &nbsp;
                        </td>
                        <td class="style110">
                            <asp:DropDownList ID="ddDivision" runat="server" Width="150px">
                            </asp:DropDownList>
                            <asp:CascadingDropDown ID="ddDivision_CascadingDropDown" runat="server" BehaviorID="divisionajax"
                                Category="Division" Enabled="True" LoadingText="Loading Divisions" ParentControlID="ddCircle"
                                PromptText="Select a Division" ServiceMethod="GetDivision" ServicePath="~/Common/WebService/GetMasters.asmx"
                                TargetControlID="ddDivision">
                            </asp:CascadingDropDown>
                        </td>
                        <td class="style110">
                            &nbsp;
                        </td>
                        <td class="style38">
                            <asp:Label ID="lblRange" runat="server" Text="Range"></asp:Label>
                        </td>
                        <td class="style117">
                            &nbsp;
                        </td>
                        <td class="style98">
                            <asp:DropDownList ID="ddRange" runat="server" Width="150px">
                            </asp:DropDownList>
                            <asp:CascadingDropDown ID="ddRange_CascadingDropDown" runat="server" BehaviorID="rangeajax"
                                Category="Range" Enabled="True" LoadingText="Loading Range " ParentControlID="ddDivision"
                                PromptText="Select a Range" ServiceMethod="GetRange" ServicePath="~/Common/WebService/GetMasters.asmx"
                                TargetControlID="ddRange">
                            </asp:CascadingDropDown>
                        </td>
                    </tr>
                    <tr>
                        <td class="style68">
                            <asp:Label ID="lblSection" runat="server" Text="Section"></asp:Label>
                        </td>
                        <td class="style115">
                            &nbsp;
                        </td>
                        <td class="style23">
                            <asp:DropDownList ID="ddSection" runat="server" Width="150px">
                            </asp:DropDownList>
                            <asp:CascadingDropDown ID="ddSection_CascadingDropDown" runat="server" Category="Section"
                                Enabled="True" LoadingText="Loading Sections" ParentControlID="ddRange" PromptText="Select Section"
                                ServiceMethod="GetSection" ServicePath="~/Common/WebService/GetMasters.asmx"
                                TargetControlID="ddSection">
                            </asp:CascadingDropDown>
                        </td>
                        <td class="style23">
                            &nbsp;
                        </td>
                        <td class="style67">
                            <asp:Label ID="lblBeat" runat="server" Text="Beat"></asp:Label>
                        </td>
                        <td class="style116">
                            &nbsp;
                        </td>
                        <td class="style110">
                            <asp:DropDownList ID="ddBeat" runat="server" Width="150px">
                            </asp:DropDownList>
                            <asp:CascadingDropDown ID="ddBeat_CascadingDropDown" runat="server" Category="Beat"
                                Enabled="True" LoadingText="Loading Beats" ParentControlID="ddSection" PromptText="Select Beat"
                                ServiceMethod="GetBeat" ServicePath="~/Common/WebService/GetMasters.asmx" TargetControlID="ddBeat">
                            </asp:CascadingDropDown>
                        </td>
                        <td class="style110">
                            &nbsp;
                        </td>
                        <td class="style38">
                            &nbsp;
                        </td>
                        <td class="style117">
                            &nbsp;
                        </td>
                        <td class="style98">
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td class="style68">
                            <asp:Label ID="lblYear" runat="server" Text="Year"></asp:Label>
                        </td>
                        <td class="style115">
                            &nbsp;
                        </td>
                        <td class="style23">
                            <asp:DropDownList ID="ddPlntYear" runat="server" Width="150px">
                                <asp:ListItem Value="0">Select Year</asp:ListItem>
                                <asp:ListItem>2015</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td class="style23">
                            &nbsp;
                        </td>
                        <td class="style67">
                            &nbsp;
                        </td>
                        <td class="style116">
                            &nbsp;
                        </td>
                        <td class="style110">
                            &nbsp;
                        </td>
                        <td class="style110">
                            &nbsp;
                        </td>
                        <td class="style38">
                            &nbsp;
                        </td>
                        <td class="style117">
                            &nbsp;
                        </td>
                        <td class="style98">
                            &nbsp;
                        </td>
                    </tr>
                </table>
                <br />
            </asp:Panel>
            <br />
            <div align="center">
                <asp:Button ID="btnSearch" runat="server" Height="32px" Text="Download" Width="106px"
                    BackColor="#365672" BorderStyle="Solid" BorderWidth="1px" Font-Bold="True" Font-Italic="True"
                    ForeColor="White" OnClick="btnSearch_Click1" />
            </div>
            <br />
            <asp:Label ID="lblMsg" runat="server"></asp:Label>
            <br />
        </ContentTemplate>
    </asp:UpdatePanel>
    <div id="Progress">
        <asp:UpdateProgress runat="server" ID="PageUpdateProgress" AssociatedUpdatePanelID="updatepanel1"
            DisplayAfter="3" DynamicLayout="False">
            <ProgressTemplate>
                <img alt="Generating KML File" src="Images/progressbar.gif" />
            </ProgressTemplate>
        </asp:UpdateProgress>
    </div>

更改您的 jquery 代码如下:

<script type="text/javascript">
        $(function() {
            setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
            $(document).on("click",'#btnSearch', function() {
                $('#Progress').show();
                setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
            })
        })
    </script>

这里我们使用了event delegation技术。由于更新面板;你的旧 html 正在被新的更新。因此,之前附加的 event listener 被清除了。使用上述技术,listener 将附加到 document,如果 html

可用,将委托给 #btnSearch

像这样在 JavaScript/jQuery 中使用 pageload

function pageLoad(sender, args) { 
            setTimeout(function () { $("#Progress").fadeOut(1500); }, 6000)
            $('#btnSearch').click(function() {
                $('#Progress').show();
                setTimeout(function () { $("#Progress").fadeOut(15000); }, 6000)
            })
        }

您需要将代码放入 endRequest 以及您正在使用的 UpdatePanel 所以请尝试以下代码:

<script type="text/javascript">
    $(function() {
        setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
        $('#btnSearch').click(function() {
            $('#Progress').show();
            setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
        })
    })

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function () {
        setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
        $('#btnSearch').click(function() {
            $('#Progress').show();
            setTimeout(function() { $("#Progress").fadeOut(1500); }, 6000)
        })
    });
</script>
<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);

    function InitializeRequest(sender, args) {
        var updateProgress = $get('UpdateProgress1');
        var postBackElement = args.get_postBackElement();
        if (postBackElement.id == '<%= Button1.ClientID %>') {
            updateProgress.control._associatedUpdatePanelId = 'dummyId';
        }
        else{
            updateProgress.control._associatedUpdatePanelId = null;
        }
    }

</script>