简体   繁体   English

想要在下拉列表选择 ASP.NET 后在客户端显示 label 'Wait, reading data.....'

[英]Want to show label 'Wait, reading data.....' in client after dropdownlist selection ASP NET

I have an ASP.NET page with a dropdownlist.我有一个带有下拉列表的 ASP.NET 页面。 When the user select an item, a postback occurs, but it takes some time to receive the response, due to server processing.当用户 select 一个项目时,会发生回发,但由于服务器处理需要一些时间才能收到响应。 I want to show a label 'wait, reading data' or something, on the client side while waiting for the response.我想在客户端等待响应时显示 label '等待,读取数据' 之类的东西。 Have tried some samples but nothing, any help?尝试了一些样品但没有任何帮助?

this, does not work这个,不起作用

<script type="text/javascript" language="javascript">
    function ShowConfirm(obj) {
        document.getElementById("<%=Lbl_Buscar.ClientID%>").style.display = "none";
        __doPostBack(obj.id, '');
        }
</script>

Actually, you can do this very easy with a button click, or in your case a dropdown.实际上,您可以通过单击按钮或在您的情况下使用下拉菜单来轻松完成此操作。

The simple trick is that a button click, or even a dropdown can have BOTH a client side event, and the server side event.简单的技巧是单击按钮,甚至下拉菜单都可以同时具有客户端事件和服务器端事件。

This works for a button, or in this case a drop down.这适用于按钮,或者在本例中是下拉菜单。

so say this markup:所以说这个标记:

<div style="float:left;margin-top:18px">
    <asp:DropDownList ID="cboHotels" runat="server"
        Width="200px"
        AutoPostBack="True"
        OnSelectedIndexChanged="LBHotels_SelectedIndexChanged"
        DataValueField="City"
        DataTextField="City"
        onchange="cbowait()">
    </asp:DropDownList>
</div>

<div id="mywait" style="float:left;margin-left:25px;display: none">
    <img src="../Content/wait2.gif" style="height: 62px; width: 75px" />
    <asp:Label ID="Label1" runat="server" Text="Please wait..." 
        Font-Size="Large"></asp:Label>
</div>
<script>
    function cbowait() {
        $('#mywait').show()
    }
</script>
<div style="clear:both"></div>
<br />

<asp:GridView ID="GridView1" runat="server" 
    CssClass="table table-hover" Width="35%">

</asp:GridView>

We have a dropdown list (combo box), then a "div" with text, picture (animated gif), or whatever for the please wait message.我们有一个下拉列表(组合框),然后是一个带有文本、图片(动画 gif)或任何内容的“div”,用于显示请稍候消息。

but, note how we have both a client side, and server side event for the drop down list.但是,请注意我们如何为下拉列表同时提供客户端和服务器端事件。

So, code behind is really simple.因此,隐藏代码非常简单。 (since the post-back WHEN complete will re-load the page - and thus re-hide our message). (因为回发 WHEN complete 将重新加载页面 - 从而重新隐藏我们的消息)。

So, code behind is this:所以,背后的代码是这样的:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // load up our combo box
        string strSQL =
            "select City from City order by City";
        SqlCommand cmdSQL = new SqlCommand(strSQL);
        cboHotels.DataSource = MyRstP(cmdSQL);
        cboHotels.DataBind();
        cboHotels.Items.Insert(0, new ListItem("Select hotel", ""));

    }

}

DataTable MyRstP(SqlCommand cmdSQL)
{
    DataTable rstData = new DataTable();
    using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
    {
        cmdSQL.Connection = conn;
        using (cmdSQL)
        {
            conn.Open();
            rstData.Load(cmdSQL.ExecuteReader());
        }
    }
    return rstData;
}


protected void LBHotels_SelectedIndexChanged(object sender, EventArgs e)
{
    string strSQL = 
        @"SELECT FirstName, LastName, City, HotelName, Description
        FROM tblhotels WHERE City = @City";
    SqlCommand cmdSQL = new SqlCommand(strSQL);
    cmdSQL.Parameters.Add("@City", SqlDbType.NVarChar).Value = cboHotels.Text;
    GridView1.DataSource = MyRstP(cmdSQL);
    GridView1.DataBind();

    System.Threading.Thread.Sleep(2000); // fake long delay
}

So, we "fake a 2 second delay所以,我们“假装延迟 2 秒

the result is thus this:结果是这样的:

在此处输入图像描述

So, all you do is add a "div" with say a spinner and some text like "please wait...".因此,您所做的就是添加一个带有微调器的“div”和一些文本,例如“请稍候...”。

And note how ZERO changes was required to the server side (code behind) here.并注意此处的服务器端(代码隐藏)如何需要零更改。

I do this all the time for buttons, and the approach is just the same.我一直为按钮做这个,方法是一样的。

Eg this:例如:

    <asp:Button ID="Button1" runat="server" Text="Button"
        OnClick="Button1_Click"
        OnClientClick="cbowait();return true;"
        />

note how for the button, we MUST return "true" for the server side event to run.请注意对于按钮,我们必须返回“true”以使服务器端事件运行。 (and this feature can be rather handy, since you can then launch a js "confirm" dialog, and if the user hits no then you return false, and the server side button click does not run. Great for say a delete button. (并且此功能非常方便,因为您可以启动一个 js“确认”对话框,如果用户点击否,则返回 false,并且服务器端按钮单击不会运行。非常适合说一个删除按钮。

Thanks for your detailed response.感谢您的详细回复。 The solution isnt working for me, after select the item in the dropdown, the page does not postback, not even display the mesage.该解决方案对我不起作用,在 select 下拉列表中的项目之后,页面不回发,甚至不显示消息。 I think it is because is inside a panel with a modalpopup extender.我认为这是因为在带有 modalpopup 扩展器的面板内。 In fact, for the dropdown to work, I have to remove the onchange part.事实上,为了使下拉菜单起作用,我必须删除 onchange 部分。

                        <asp:Button ID="BtnShowPopUp4" runat="server" Text="Button" style="display:none" />
                    <cc1:ModalPopupExtender ID="ModalPopupExtenderNC" runat="server" 
                            BackgroundCssClass="modalBackground" 
                            DropShadow="True" 
                            PopupControlID="PanelNC" TargetControlID="BtnShowPopUp4"  CancelControlID="BtnCancelNC" >
                    </cc1:ModalPopupExtender>   
                    <asp:Panel defaultbutton="BtnOkNC" ID="PanelNC" runat="server" CssClass="modalPopup" 
                        Width="585px"  >
                        <span style="text-decoration: underline; font-size: large">SOLICITUD NOTA DE CREDITO</span>&nbsp;&nbsp;&nbsp;&nbsp;<br />
                        &nbsp;<table style="width: 100%">
                            <tr>
                                <td style="width: 122px; text-align: left; ">&nbsp;</td>
                                <td style="text-align: left">
                                    <asp:RadioButton ID="Rdr_USNC" runat="server" GroupName="Moneda" Text="US$" AutoPostBack="True" OnCheckedChanged="Rdr_USNC_CheckedChanged" />
                                    <asp:RadioButton ID="Rdr_RDNC" runat="server" GroupName="Moneda" Text="RD$" AutoPostBack="True" OnCheckedChanged="Rdr_USNC_CheckedChanged" />
                                    &nbsp;
                                    <div id="mywait" style="display: none">
                                            <asp:Label ID="Lbl_Buscar" runat="server" style="font-weight: 700; color: #CC0000" Text="**Buscando Datos" ></asp:Label>
                                   </div>
                                    
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 122px; text-align: left; background-color: #74D1A3;"><b>
                                    <asp:Label ID="Label4" runat="server" style="font-family: Arial, Helvetica, sans-serif; font-size: small" Text="Cliente"></asp:Label>
                                    &nbsp;<asp:HyperLink ID="Hyp_Cod_Cliente" runat="server" Font-Bold="False" style="font-size: small"></asp:HyperLink>
                                    </b></td>
                                <td style="text-align: left">
                                    <asp:DropDownList ID="Cb_Clientes" runat="server"  AutoPostBack="True" DataSourceID="DS_Clientes" DataTextField="wNombre" 
                                        DataValueField="Codigo" Font-Names="Arial" Font-Size="9pt" Height="25px"  Width="350px"
                                         OnSelectedIndexChanged="Cb_Clientes_SelectedIndexChanged" onchange="cbowait()"  >
                                    </asp:DropDownList>
                                    <script type="text/javascript">
                                        function cbowait() {
                                            $('#mywait').show()
                                        }
                                    </script>           
                                </td>
                            </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM