代码之家  ›  专栏  ›  技术社区  ›  Tola

在下拉列表的SelectedIndexChanged事件上显示加载消息

  •  0
  • Tola  · 技术社区  · 16 年前

    当用户在DORP下拉列表中选择一个项目时,我试图显示消息“正在加载…”。

    标记:

    <asp:Label ID="lbl_LoadingMessage" runat="server" ></asp:Label>
    
    <asp:DropDownList ID="ddl_Chapter" runat="server" AutoPostBack="True">
                </asp:DropDownList>
    

    代码落后:

    Protected Sub LoadMessage()
            lblLoading.Text = "Loading..."
    End Sub
    
    
    Protected Sub ddl_Chapter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_Chapter.SelectedIndexChanged
    
            LoadMessage()
    
            Dim redirectURL As String = "~/chapter.aspx?bid=" & BookId.ToString
            Server.Transfer(redirectURL)
    
    End Sub
    

    我上面使用的方法不起作用。当我从下拉列表中选择一个新项目时,它会像预期的那样工作,除了“正在加载…”一点也不显示。有什么建议或代码示例吗?谢谢您。

    3 回复  |  直到 16 年前
        1
  •  1
  •   Paul    16 年前

    您必须在客户端使用JavaScript来完成这项工作。

    目前,您的下拉菜单正在引起回发。当下拉菜单更改时,页面文章将后退,然后整个页面生命周期将运行。当事件ddl_章节“selectedindexchanged”运行时,您设置了加载标签的文本,但从不重新加载页面(这将有您的加载消息),而是将server.transfer改为新页面。

    如果使用jquery,则可以在下拉列表更改后立即设置标签文本值。

    类似:

    $('#the_full_renedered_ID_of_ddl_Chapter').change(function () {
          $('#the_full_renedered_ID_of_lbl_LoadingMessage').html("Loading...")
    });
    
        2
  •  0
  •   user187029    16 年前

    或者使用javascript:

    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" /></script>
    <script type="text/javascript">
    $(document).ready( function() {
        $("#<%= ddl_Chapter.ClientID %>").change(function()
        {
            window.alert("Loading");
        });     
    });
    </script>
    
        3
  •  0
  •   w4ymo    16 年前

    整个事件将在重新呈现页面之前执行。

    如果您要在 LoadMessage() 以及 Server.Transfer 尝试使用Ajax UpdateProgress面板并添加“正在加载…”消息,然后将DropDownList添加到UpdatePanel。

    这取决于您的 SelectedIndexChanged 事件它将通过部分页面回发显示“正在加载…”消息。

    例如

        <asp:ScriptManager id="ScriptManager1" runat="server">
        </asp:ScriptManager>
    
        <asp:UpdateProgress id="UpdateProgress1" runat="server" associatedUpdatePanelID="UpdatePanel1">
            <ProgressTemplate>
                <p>Loading....</p>
            </ProgressTemplate>
        </asp:UpdateProgress>
    
        <asp:UpdatePanel id="UpdatePanel1" runat="server" childrenAsTriggers="true">
            <ContentTemplate>
                <asp:DropDownList id="DropDownList1" runat="server" autoPostBack="true">
                    <asp:ListItem selected="True" value="1">Book 1</asp:ListItem>
                    <asp:ListItem value="2">Book 2</asp:ListItem>
                    <asp:ListItem value="3">Book 3</asp:ListItem>
                    <asp:ListItem value="4">Book 4</asp:ListItem>
                    <asp:ListItem value="5">Book 5</asp:ListItem>
                </asp:DropDownList>
    
                <asp:Label id="lblSelected" runat="server"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
    

    这样,“正在加载…”消息将在处理您在 选择的索引已更改 事件。如果这仅仅是出于显示的原因,那么JavaScript将是您的最佳选择。