简体   繁体   English

如何在 ASP.NET 中动态更改按钮文本

[英]How change button text dynamically in ASP.NET

I want to change my button text dynamically using c# in asp.net.我想在 asp.net 中使用 c# 动态更改我的按钮文本。 I also tried dynamic javascript alert popup, however it also shows after mail sent.我还尝试了动态 javascript 警报弹出窗口,但它也会在邮件发送后显示。

Currently i have;目前我有;

 protected void btnSend_Click(object sender, EventArgs e)
    {
        btnSend.Text = "Sending.."; // Changing Button text
    ScriptManager.RegisterStartupScript(btnSend,GetType(), "Javascript", "javascript:helloWorld(); ", true); // Also popup javascript test output

         using (MailMessage mm = new MailMessage("info@oratify.com", "info@oratify.com"))
        {

            mm.Subject = "Dropped Mails";
            string body = Request.Form["email"];
            body+="<br>" + "<br>" + Request.Form["message"];
            mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.office365.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential("info@oratify.com", "Bensezer10.");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
   }

and in aspx side;在 aspx 端;

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <section class="cid-qKKEwJNZ1Q mbr-fullscreen mbr-parallax-background" id="header15-2p">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>

                                        <div>
                                            <table style="width: 100%; text-align: center; margin-top: 0px; height: 78px;">
                                                <tr>
                                                    <td style="text-align: center;">
                                                        <asp:Button ID="btnSend" runat="server" Text="SEND" Width="200px" BorderStyle="None" Height="60px" Font-Names="SF Pro Display" Font-Size="14pt" CssClass="ButtonClass" OnClick="btnSend_Click" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnSend" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
        </section>
    </form>

Well, it changes the buttontext after sending mail function is completed which is 3 second later.好吧,它会在发送邮件功能完成后更改按钮文本,即 3 秒后。

How can i make this dynamically?我怎样才能动态地做到这一点?

You're changing the button text while the server is still processing the request.当服务器仍在处理请求时,您正在更改按钮文本。 Only when the email has sent does the client receive the server's response and can actually display the results.只有当电子邮件发送后,客户端才会收到服务器的响应,才能真正显示结果。

What you'd need to do is use JQuery/Javascript to change the button text before initiating a post back to the server via an ajax request and then update the text back once the server has finished sending the emails.您需要做的是在通过 ajax 请求启动回发到服务器之前使用 JQuery/Javascript 更改按钮文本,然后在服务器完成发送电子邮件后更新文本。

There's a much more detailed How To (as there are a fair few steps involved), to be found here .可以在此处找到更详细的操作方法(因为涉及的步骤很少)。

You need to use javascript to update the button text.您需要使用 javascript 来更新按钮文本。 In its simplest form, add this OnClientClick="this.value = 'Sending....';"在最简单的形式中,添加这个OnClientClick="this.value = 'Sending....';" to your button.到你的按钮。

<asp:Button ID="btnSend" OnClientClick="this.value = 'Sending....';" runat="server" Text="SEND" Width="200px" BorderStyle="None" Height="60px" Font-Names="SF Pro Display" Font-Size="14pt" CssClass="ButtonClass" OnClick="btnSend_Click" />

And in the code-behind:在代码隐藏中:

protected void btnSend_Click(object sender, EventArgs e)
{
    using (MailMessage mm = new MailMessage("info@oratify.com", "info@oratify.com"))
    {
        // do your things...
    }
    btnSend.Text = "Sent";
}

In Chrome, I've seen that this.value = '...';在 Chrome 中,我看到this.value = '...'; does not work when the page is rendered by IIS Express in Visual Studio 2019. In my case, what worked is:当页面由 Visual Studio 2019 中的 IIS Express 呈现时不起作用。就我而言,起作用的是:

<asp:LinkButton ID="myButton" runat="server" OnClick="doSomething()" OnClientClick="this.style.background='#36648B';this.text='Wait...';">Click!</asp:LinkButton>

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

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