仅在 C# 方法成功后显示元素 运行

Only show element after C# method has successfully run

我正在 ASP.NET 框架中创建一个联系页面,我试图在发送电子邮件后显示一个框,但我不知道如何仅在方法具有 运行,我可以让它在短时间后消失,但每次我刷新页面时,框都会回来。这是我想出的:

只有当我的 Clickfunction 有 运行:

时,我才想显示我的提交按钮和 Label1
        <asp:TextBox ID="txtName" runat="server" placeholder="Namn"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ErrorMessage="Fyll i Namn" ControlToValidate="txtName"></asp:RequiredFieldValidator>
        <asp:TextBox ID="txtEmail" runat="server" placeholder="E-mail"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Fyll i Email" ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" ID="RegularExpressionValidator1" runat="server" ErrorMessage="Felaktig Email" ControlToValidate="txtEmail"></asp:RegularExpressionValidator>
        <input style="display: none" type="text" name="phone" />
        <asp:TextBox ID="txtPhone" runat="server" placeholder="Telefon" AutoCompleteType="HomePhone"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Fyll i Telefon" ControlToValidate="txtPhone"></asp:RequiredFieldValidator>
        <asp:TextBox ID="txtMessage" runat="server" placeholder="Meddelande" TextMode="MultiLine" Rows="8"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Fyll i Meddelande" ControlToValidate="txtMessage"></asp:RequiredFieldValidator>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Skicka" OnClick="Button1_Click" OnClientClick="disableElement()" UseSubmitBehavior="false" />
        <br />
        <div id="labelBox">
            <asp:Label ID="Label1" runat="server" Text="Tack för ditt meddelande!"></asp:Label>
        </div>

我的点击功能:

protected void Button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Clicked");
            try
            {
                if (Page.IsValid)
                {
                    MailMessage mailMessage = new MailMessage();
                    mailMessage.From = new MailAddress(WebConfigurationManager.AppSettings[""]);
                    mailMessage.To.Add(WebConfigurationManager.AppSettings[""]);
                    mailMessage.Subject = "Förfrågan";
                    mailMessage.Body = "Namn: " + txtName.Text + "<br />" + "Telefon: " + txtPhone.Text + "<br />" + txtMessage.Text;
                    mailMessage.IsBodyHtml = true;

                    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                    smtpClient.EnableSsl = true;
                    smtpClient.Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings[""], WebConfigurationManager.AppSettings[""]);

                    smtpClient.Send(mailMessage);

                    txtName.Text = String.Empty;
                    txtEmail.Text = String.Empty;
                    txtPhone.Text = String.Empty;
                    txtMessage.Text = String.Empty;
                    

                }
            } catch(Exception ex)
            {
                Label1.Text = "Problem" + ex;
            } 
        }

认为您可能正在寻找“可见”属性。

<asp:Label ID="Label1" runat="server" Visible="False" Text="Tack för ditt meddelande!"></asp:Label>
    

然后当你想让它在客户端可见时就做

Label1.Visible = true;

在您的事件处理程序中。

“可见”属性 完全阻止控件被推送到客户端 PC。如果您希望控件存在于客户端,但被隐藏,最简单的方法可能是定义一个 CSS class

.hidden { display: none; }

然后使用 CssClass 属性

Label1.CssClass = "";

<asp:Label ID="Label1" runat="server" CssClass="hidden" Text="Tack för ditt meddelande!"></asp:Label>

到 show/hide 控件。