简体   繁体   English

如何在页面重定向后发布消息以确认已发送的消息(C#/ ASP.Net)

[英]How do I post a message to confirm a sent message after a page redirect (C#/ASP.Net)

Hi I've just set up a method in my website to send an email from a Contact Us page, I wanted to clear the asp.net form of all of the email details once they have sent the email so I've used a response.redirect to refresh the page as such. 嗨,我刚刚在我的网站上设置了一个方法,从联系我们页面发送电子邮件,我想在发送电子邮件后清除所有电子邮件详细信息的asp.net表单,以便我使用了回复.redirect以刷新页面。 I want to display a message or a pop up window to show that the message has been sent, but because the page is being refreshed the label I am trying to write this to never gets called. 我想显示一条消息或一个弹出窗口来显示消息已被发送,但由于页面正在刷新,我试图写这个标签永远不会被调用。 Is there any way around this or a way to clear the form without a redirect? 有没有办法绕过这个或一种方法来清除没有重定向的表单? Here is the code I am working with: 这是我正在使用的代码:

protected void SubmitBtn_Click(object sender,EventArgs e)
{
    MailMessage mailObj = new MailMessage(
       EmailTxt.Text, "xxxxx@xxxxx.com", SubjectTxt.Text, MessageTxt.Text);
    SmtpClient SMTPServer = new SmtpClient("localhost");
    try
    {
        SMTPServer.Send(mailObj);
        Response.Redirect("ContactMe.aspx");
        base.OnLoad(e);
        MessageLbl.Text = "Email Sent SucessFully.";
    }
    catch (Exception ex)
    {
        MessageLbl.Text = ex.ToString();
    }
}

Many Thanks in advance 提前谢谢了

If you didn't want your user left on the email page (assuming you want to take them somewhere else after they send their email) you could use: 如果您不希望您的用户留在电子邮件页面上(假设您希望在发送电子邮件后将其带到其他地方),您可以使用:

Response.AddHeader("REFRESH","3;URL=yourURL.aspx"); 

Instead of 代替

Response.redirect. 

Your label will pop up with the message and then the redirect will happen a few seconds later. 您的标签会弹出消息,然后重定向将在几秒钟后发生。

The Response.Redirect is your problem. Response.Redirect是你的问题。 You are effectively refreshing the page and any code after that is irrelevant. 您正在有效刷新页面,之后的任何代码都无关紧要。 Why not just clear the form via clode (ie myTextBox.Text = string.Empty etc. and then set the message text? 为什么不通过clode清除表单(即myTextBox.Text = string.Empty等,然后设置消息文本?

try
{
    SMTPServer.Send(mailObj);
    // Clear email form
    Control1.Text = string.Empty;
    Control2.Text = string.Empty;
    // etc...
    MessageLbl.Text = "Email Sent SucessFully.";
}
catch (Exception ex)
{
    MessageLbl.Text = ex.ToString();
}

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

相关问题 如何将消息发布到仅包含用户发布的消息列表的留言板页面 ASP.NET WebForm - How do I post a message to a message board page that just contains a list of messages posted from users ASP.NET WebForm 如何在不使用表单的情况下以C#在ASP.NET中创建消息表单 - How do I Make a message form in ASP.NET in C# without using a form 如何在 ASP.NET 中显示警报消息并重定向到主页 - How to display the alert message and redirect to home page in ASP.NET 在C#asp.net webform中显示消息和重新加载页面 - Show message and Reload page in C# asp.net webform 如何在ASP.Net应用程序作为电子邮件发送的短信中包含换行符? - How do I include a newline in a text message sent as email from an ASP.Net application? ASP.NET执行JavaScript仅在条件满足时才在C#代码中确认警报消息 - ASP.NET Executing JavaScript Confirm alert message in C# code behind only if condition is satisfied 如何在asp.net c#中使用带有警报消息的window.location从文件夹重定向回来 - How to redirect back from folder by window.location with alert message in asp.net c# 使用 C# RSS 在 ASP.NET 中的消息框后刷新页面 - Refresh page after Message box in ASP.NET using C# RSS 页面重定向ASP.NET C#后不显示数据 - Data is not displayed after Page Redirect ASP.NET C# ASP.Net复选框确认消息不起作用 - ASP.Net Checkbox Confirm Message not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM