如何在页面加载完成之前使div可见



我有一个使用WebForms在ASP.NET中构建的页面。

我有几个UpdatePanel,它们似乎工作得很好。但我遇到了一个问题。

我有一个组合框,当值更改时,它会连接到TFS并检索项目的详细信息,以便填充列表框。这可能需要一些时间才能完成,因为我们的TFS服务器在澳大利亚,而我在英国,所以我想我应该在详细信息中显示一个小图形来向用户指示它的加载情况。

这里有一个我目前所拥有的例子:

HTML:

<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:Panel ID="InnerPannelLeft" runat="server" CssClass="innerContentPanel_Left">
    <asp:Panel ID="Panel2" runat="server" CssClass="innerContentPanel_Border">
        <asp:Panel ID="Panel3" runat="server" CssClass="runResultsPanels">
            <asp:Label ID="Label2" runat="server" Text="Test Suite:  "></asp:Label>
            <asp:DropDownList ID="TestSuite" runat="server" OnSelectedIndexChanged="TestSuite_SelectedIndexChanged" AutoPostBack="True">
                <asp:ListItem Selected="True">Select a Test Suite</asp:ListItem>
                <asp:ListItem>Trades</asp:ListItem>
                <asp:ListItem>Dividends</asp:ListItem>
            </asp:DropDownList>
        </asp:Panel>
    </asp:Panel>
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="TestSuite" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:Panel ID="LoadingPanel" CssClass="innerContentPanel_Border" runat="server" Visible="false">
            <asp:Panel ID="TimeDiv" runat="server">
                <asp:Label ID="Label6" runat="server" Text="Loading..."></asp:Label>
                <asp:Panel ID="TimerAnimation" CssClass="timer" runat="server"></asp:Panel>
            </asp:Panel>
        </asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>

C#:

protected void TestSuite_SelectedIndexChanged(object sender, EventArgs e)
{
    if (TestSuite.SelectedIndex > 0)
    {
         //Show the loading div
         LoadingPanel.Visible = true;
         //Long running code that grabs stuff from TFS
         //Grabbing stuff from TFS has finished so hide the div
         //LoadingPanel.Visible = false;
    }
}

这就是代码。但基本上,在combobox更改的方法中的处理完成之前,它不会显示div。

有没有办法让div在combobox开始做所有长期运行的事情之前立即显示出来?

我认为将div放在更新面板中并添加触发器会允许它异步更新面板吗?如果我错了,请原谅我的无知,MSDN网站让我很困惑。

编辑:我添加了一些JQuery,它可以显示基于onchange事件的警报。但它不会使div可见。

这是代码:

$(document).ready(function ()
{
    $("#<%=TestSuite.ClientID%>").change(function()
    {
        if ($(this).find('option:selected').text() === "Select a Test Suite")
        {
            alert("Hide the panel");
            $("#<%=LoadingPanel.ClientID%>").hide();
        }
        else
        {
            alert("Show the panel");
            $("#<%=LoadingPanel.ClientID%>").show();
        }
    })
})

我猜这是因为回邮而忘记了更改?DIV的默认可见性设置为false。

所以我认为每次回发(也就是每次我更改组合)都会回到默认状态。还是我来错地方了?

TestSuite_SelectedIndexChanged是一个服务器端事件,因此在回发之前不会显示任何内容。在javascript或jquery启动之前,您需要在客户端执行一些操作,或者您可以添加AjaxUpdate控件:http://www.asp.net/ajax/documentation/live/overview/updateprogressoverview.aspx

最新更新