[英]Reset form and page state after 5 seconds in ASP.NET
I have a form that the user submits and gets a Success -message. 我有一个用户提交的表单并获得Success -message。 Now I need to redirect back to the form and empty it , but I cannot seem to get the page to refresh itself after 5 seconds.
现在我需要重定向回到表单并清空它 ,但我似乎无法在5秒后让页面自动刷新。
This is what I've got so far. 这是我到目前为止所得到的。 With this I get
Response is not available in this context
, which means I need some other way to refresh. 有了这个,我得到
Response is not available in this context
,这意味着我需要一些其他方式来刷新。
protected void bSubmit_Click(object sender, EventArgs e)
lblFeedback.Text = "Your message has been sent.";
lblFeedback.Visible = true; // Show the success-message
mainForm.Visible = false; // Hide the form the user just filled in
reTimer = new System.Timers.Timer(1000 * 5);
reTimer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshToForm);
reTimer.Enabled = true;
}
private void RefreshToForm(object sender, System.Timers.ElapsedEventArgs e)
{
reTimer.Enabled = false;
reTimer = null;
Response.Redirect(Request.RawUrl);
}
Try adding a Refresh
header in the Response instead of using a Timer: 尝试在Response中添加
Refresh
标头而不是使用Timer:
protected void bSubmit_Click(object sender, EventArgs e)
lblFeedback.Text = "Your message has been sent.";
lblFeedback.Visible = true; // Show the success-message
mainForm.Visible = false; // Hide the form the user just filled in
Response.AddHeader("Refresh", "5"); // Refresh the page after 5 seconds
}
A server-side timer won't work - by the time it triggers, the request has probably already finished and returned to the client, hence why you can't get the Request object, it's out of context. 服务器端计时器不起作用 - 当它触发时,请求可能已经完成并返回给客户端,因此为什么你不能获取Request对象,它不在上下文中。
One option is to simply clear all the form controls out, rather than waiting and then refreshing (I imagine that's a bit confusing for the user, plus it will destroy your "Feedback" message, which they might not see if they don't immediately look at the page within 5 seconds). 一种选择是简单地清除所有表单控件,而不是等待然后刷新(我想这对用户来说有点混乱,加上它会破坏你的“反馈”消息,如果他们不立即就可能看不到在5秒内查看页面)。
Here's one I made earlier: 这是我之前制作的一个:
/// <summary>
/// Reset input fields within a collection of Controls back to their empty/cleared states
/// </summary>
/// <param name="ctrls"></param>
/// <remarks>Will clear textboxes, dropdownlists and checkboxes. Checkboxes will be reset to un-checked.</remarks>
private void ClearInputs(ControlCollection ctrls)
{
foreach (Control ctrl in ctrls) {
if (ctrl is TextBox) {
((TextBox)ctrl).Text = string.Empty;
} else if (ctrl is DropDownList) {
((DropDownList)ctrl).ClearSelection();
} else if (ctrl is CheckBox) {
((CheckBox)ctrl).Checked = false;
} else if (ctrl is HiddenField) {
((HiddenField)ctrl).Value = string.Empty;
}
ClearInputs(ctrl.Controls);
}
}
In your case, you can call it like this: 在您的情况下,您可以这样调用它:
ClearInputs(mainForm.Controls);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.