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

标签 c# asp.net

我想在 asp.net 中使用 c# 动态更改我的按钮文本。我还尝试了动态 javascript 警报弹出窗口,但它也会在邮件发送后显示。

目前我有;

 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);
        }
   }

在 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>

嗯,它在发送邮件功能完成后更改按钮文本,即 3 秒后。

我怎样才能动态地做到这一点?

最佳答案

当服务器仍在处理请求时,您正在更改按钮文本。客户端只有在邮件发送完毕后,才能收到服务器的响应,才能真正显示结果。

您需要做的是使用 JQuery/Javascript 在 开始通过 ajax 请求发回服务器之前更改按钮文本,然后在服务器完成后更新文本发送电子邮件。

可以找到更详细的操作方法(因为涉及的步骤很少)here .

关于c# - 如何在 ASP.NET 中动态更改按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49017692/

相关文章:

c# - ASP.NET 错误处理问题

c# - 在数据库中更改表时是否可以刷新 gridview?

c# - 关于缓存的线程安全 IEnumerable<T> 实现的性能

c# - 如何缩放字体以适合指定的矩形

asp.net - 如何捕获或拦截ODataException以返回自定义错误有效载荷

asp.net - 可以通过ASP.NET页面配置Quartz.NET日历吗?

asp.net - 来自 ASP.NET 的 COM 互操作

c# - 为什么我不能在类上使用 ConditionalAttribute?

c# - 查询处理器无法启动并行查询执行所需的线程资源

c# - 具有合并单元格的 GridView