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

C# 如何在ASP.NET中动态更改按钮文本,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(), "

我想在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);
        }
   }
protectedvoidbtnsend\u单击(对象发送者,事件参数e)
{
btnSend.Text=“发送..”;//更改按钮文本
ScriptManager.RegisterStartupScript(btnSend,GetType(),“Javascript”,“Javascript:helloWorld();”,true);//也会弹出Javascript测试输出
使用(MailMessage mm=新的MailMessage(“info@oratify.com", "info@oratify.com"))
{
mm.Subject=“丢弃的邮件”;
字符串体=请求。表单[“电子邮件”];
正文+=“
”+“
”+请求。表单[“消息”]; mm.主体=主体; mm.IsBodyHtml=true; SmtpClient smtp=新SmtpClient(); smtp.Host=“smtp.office365.com”; smtp.EnableSsl=true; NetworkCredential NetworkCred=新的网络凭据(“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秒钟后)更改buttontext


如何动态执行此操作?

服务器仍在处理请求时,您正在更改按钮文本。只有当电子邮件发送后,客户端才会收到服务器的响应,并且可以实际显示结果

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


还有一个更详细的操作方法(因为涉及的步骤相当多)。

您正在更改按钮文本,而服务器仍在处理请求。只有当电子邮件发送后,客户端才会收到服务器的响应,并且可以实际显示结果

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


还有一个更详细的操作方法(因为涉及的步骤很多)。

您需要使用javascript更新按钮文本。以最简单的形式,将此
OnClientClick=“this.value=”Sending…;“
添加到按钮中

<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" />

您需要使用javascript来更新按钮文本。以最简单的形式,将此
OnClientClick=“this.value=”Sending…;“
添加到按钮中

<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" />

在Chrome中,我看到
this.value='…'在Visual Studio 2019中由IIS Express呈现页面时不起作用。就我而言,有效的方法是:

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

在Chrome中,我看到了
this.value='…'在Visual Studio 2019中由IIS Express呈现页面时不起作用。就我而言,有效的方法是:

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

使用
btnSend.Text=“发送完成”有什么问题之后
smtp.Send(mm)?啊,我明白了。你想做的事行不通。不能在一次回发中两次更改按钮文本并同时显示两个结果。您必须使用javascript将文本更改为“发送”,方法中只有一个“发送完成”。使用
btnSend.text=“发送完成”有什么错之后
smtp.Send(mm)?啊,我明白了。你想做的事行不通。不能在一次回发中两次更改按钮文本并同时显示两个结果。您必须使用javascript将文本更改为“发送”,方法中只有一个“发送完成”。我也使用了java脚本,但它仍然等待邮件发送。编辑我的问题我也使用了java脚本,但它仍然等待邮件发送。编辑我的问题