[英]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.