如何在几秒钟后隐藏此错误消息

本文关键字:隐藏 消息 错误 几秒 | 更新日期: 2023-09-27 17:57:48

我正在开发一个简单的注册系统,在该系统中,所有事件都将列在GridView中,用户可以通过单击事件旁边显示的"注册"按钮来点击事件中的注册。工作正常,但我只有一个问题,那就是:当用户试图在他已经注册的事件中注册时,他将在GridView下收到一条错误消息。当他在另一场比赛中成功注册时,问题此消息将一直显示。我想要的只是显示这条信息几秒钟。

那么该怎么做呢

以下是我在ASP.NET中的代码:

<asp:GridView ID="ListOfAvailableEvents_GrivView" runat="server" AutoGenerateColumns="False"
        DataKeyNames="ID" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
        GridLines="None" AllowPaging="True" PageSize="10" 
        onrowdatabound="ListOfAvailableEvents_GrivView_RowDataBound">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
    <Columns>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:LinkButton ID="lnkTitle" runat="server" CssClass="button" Text="Book &rarr;" OnClick="lnkTitle_Click"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
        <asp:BoundField DataField="StartDateTime" HeaderText="Start Date & Time" SortExpression="StartDateTime" />
        <asp:BoundField DataField="EndDateTime" HeaderText="End Date & Time" SortExpression="EndDateTime" />
    </Columns>
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle Font-Bold="True" CssClass="complete" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <EmptyDataTemplate><h2>No Events Available</h2></EmptyDataTemplate>
</asp:GridView>
<span id="errorSpan" runat="server" style="color:Red;"></span>

<asp:Button ID="btnModalPopUp" runat="server" Style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="modalPopupExtender1" runat="server" TargetControlID="btnModalPopUp"
    PopupControlID="pnlPopUp" BackgroundCssClass="popUpStyle" PopupDragHandleControlID="panelDragHandle"
    OkControlID="OKButton">
</ajaxToolkit:ModalPopupExtender>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Panel ID="pnlPopUp" runat="server" CssClass="popUpStyle">
    <%--<asp:Label ID="lblQuestion" runat="server" Text="Are you sure you want to register in" />--%>
    <table class="tableClass" width="400px">
        <tr>
            <th valign="middle" colspan="2" align="center" bgcolor="#99CCFF" style="font-weight: bold;
                font-size: larger">
                Event Details
            </th>
        </tr>
        <tr>
            <td class="thClass">
                Title:
            </td>
            <td class="tdClass">
                <asp:Label ID="lblTitle" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="thClass">
                Description:
            </td>
            <td class="tdClass">
                <asp:Label ID="lblDescription" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="thClass">
                Location:
            </td>
            <td class="tdClass">
                <asp:Label ID="lblLocation" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="thClass">
                Start Date & Time:
            </td>
            <td class="tdClass">
                <asp:Label ID="lblStartDateTime" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="thClass">
                End Date & Time:
            </td>
            <td class="tdClass">
                <asp:Label ID="lblEndDateTime" runat="server" />
            </td>
        </tr>
    </table>
    <center>
    <asp:Button ID="confirmButton" runat="server" Text="Register" OnClick="btnSendConfirmationEmail_Click" />
    <asp:Button ID="OKButton" runat="server" Text="Cancel" /></center>
</asp:Panel>

C#代码背后:

protected void btnSendConfirmationEmail_Click(object sender, EventArgs e)
{
    int eventID = Convert.ToInt32(HiddenField1.Value);
    if (!UserHasBooking(userNetworkID, eventID))
    {
        checkUserID(userNetworkID, eventID);
        SmtpClient sc = new SmtpClient("Mail Server");
        StringBuilder sb = new StringBuilder();
        MailMessage msg = new MailMessage();
        //Variables for retrieving the Booking Information
        string title = lblTitle.Text;
        string description = lblDescription.Text;
        string location = lblLocation.Text;
        string startDateTime = lblStartDateTime.Text;
        string endDateTime = lblEndDateTime.Text;
        //Message Information
        string toAddress = userNetworkID + "@mailserver.com";
        string fromAddress = "Test@MailServer.com";
        string mailSubject = "Registration Notification: " + title + " (" + startDateTime + " - " + endDateTime + ")";
        string messageBody = "............";
        try
        {
            msg.To.Add(toAddress);
            msg.From = new MailAddress(fromAddress, "Registration System");
            msg.Subject = mailSubject;
            msg.Body = messageBody;
            msg.IsBodyHtml = true;
            sc.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
            // something bad happened
            //Response.Write("Something bad happened!");
        }
        finally
        {
            if (msg != null)
            {
                msg.Dispose();
            }
        }
    }
    else
    {
        errorSpan.InnerText = "You already have a booking in this event";
    }
}

错误消息为:errorSpan.InerText="您已经预订了此活动";

那么如何解决呢

如何在几秒钟后隐藏此错误消息

定义函数:

function hideError(){
        setTimeout(function () {
            $("#errorSpan").hide();
        }, sec * 1000);
    }

调用其他部分中的以下语句

string javaScript =
"<script language=JavaScript>'n" +
"hideError();'n" +
"</script>";
RegisterStartupScript("hideError", javaScript); 

ClientScript.RegisterClientScriptBlock(GetType(), "close", "<script language='javascript'>hideError();</script>", false);
<script type="text/javascript">
        window.onload = function () {
            var seconds = 5;
            setTimeout(function () {
                document.getElementById("<%=lblMessage.ClientID %>").style.display = "none";
            }, seconds * 1000);
        };
    </script>

只需在标题部分添加此代码。。。。。。它有效。。

<script type="text/javascript">
        window.onload = function () {
            var seconds = 5;
            setTimeout(function () {
                document.getElementById("<%=lblreg.ClientID %>").style.display = "none";
            }, seconds * 1000);
        };
    </script>

只需在标题部分添加此代码。。。。。。它有效。