Asp.Net - 页面刷新(F5)不恢复TextBox的初始值
这是简单的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txt.Text = "Original";
}
}
第一次加载。文本框状态为“原始”。
手动,将值更改为“非原始”。
按 F5。该行:
txt.Text = "原始";
已执行,但输入值仍为“非原始”
,但是当我在地址栏中按 Enter 时。该值更改为“原始”。
更奇怪的是,当地址末尾包含“#”时(使用 jquery click..)
,那么即使我在地址栏中点击,该值仍然是“非原始”
this is the simple code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txt.Text = "Original";
}
}
first load. text box state is "Original".
manually , changing the value to "Not Original".
pressing F5. the line:
txt.Text = "Original";
is executed but the Input value remain "Not Original"
but, when I hit with enter into the adressbar. the value is changed to the "Original".
more starnge is when the adress contains '#' at the end (using jquery click..)
then, even when I hit in the adressbar, the value remain "Not Original"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您刷新 (F5) ASP.NET 页面时,它将重复上次执行的操作。因此,在您的情况下,如果您所做的最后一件事是更改文本框值,则刷新会将文本框再次设置为该值。
然而,在地址栏中点击“回车”会指示您的浏览器放弃所有内容并转到全新的页面。
“回发”意味着您将页面提交(发布)回自身。第一次加载页面时,IsPostBack 为 false,因为您只是请求该页面 - 不提交任何内容。但是,您在 ASP.NET 页面上执行的每个操作(一旦您到达该页面)都会向同一页面提交一个隐藏表单。对于那些后续请求,IsPostBack 为 true。
最后,当地址栏中有“#”时,在该 URL 上按 Enter 键不会导致页面重新加载。这是因为 # 表示锚点。如果您已经在“页面 x”上并尝试导航到“页面 x#something”,则该页面将不会重新加载 - 它将保持在浏览器中的原样,最多跳转到锚点,但不会重新加载。
When you refresh (F5) an ASP.NET page, it will repeat the last action that was taken. So in your case, if the last thing you did was change a textbox value, refreshing will set the textbox to that value again.
Hitting "enter" in the address bar, however, instructs your browser to discard everything and go to the page completely fresh and new.
"Postback" means you are submitting (posting) the page back to itself. The first time you load a page, IsPostBack is false because you're simply requesting the page - not submitting anything. But every action you take on the ASP.NET page - once you're there - is submitting a hidden form to the same page. IsPostBack is true for those subsequent requests.
Finally, when there is a "#" in your address bar, hitting enter on that URL will not cause the page to reload. This is because the # signifies an anchor. If you are already on "page x" and try to navigate to "page x#something", the page will not reload - it will stay as is in the browser, at most jump to the anchor point, but will not be reloaded.
昨天我实际上注意到 IE(7) 和 Firefox(3.5) 的差异!我在 html 中创建了一个带有一些输入字段的表单,并使用 IE,刷新导致所有字段设置回空白(默认状态),但在 FireFox 中,刷新重新加载页面(包括我所做的代码更改),但保留了我在字段中写下的值!在开发/测试时非常有用,所以我不需要每次都重写我的测试数据!
出于好奇,我刚才在 ASP.NET 中编写了一个简单的测试,我注意到您在 Firefox 中所做的相同:点击刷新按钮会保留更改的值,但在地址栏中点击 Enter 会重新加载
页面加载
。但在 IE 中,这两种情况下该值都会重置为原始值!至于不同情况之间的基本(不依赖于浏览器)差异,答案来自 Rex M 非常好。
I actually noticed a difference in IE(7) and Firefox(3.5) yesterday! I created a form in html with some input fields, and using IE, a refresh caused all feilds to be set back to blank (default state) but in FireFox, a refresh reloaded the page (including code changes I had done) but kept the values I had written in the fields! Very useful when developing/testing, so I did not need to rewrite my test data each time!
Just for curiosity, I wrote a simple test in ASP.NET just now, and I noticed the same that you do for Firefox: Hitting refresh button keeps the changed value, but hitting Enter in address bar, reloads the original text set in
Page_Load
. But in IE, the value is reset to original in both cases!As for the basic (not browser dependent) differences between the different cases, the answer from Rex M is very good.
您使用什么浏览器?某些浏览器和插件会尝试在不同情况下保留表单设置。
What browser are you using? Some browsers and plugins try to retain form settings under different circumstances.
当您刷新 (F5) ASP.NET 页面时,它将重复上次执行的操作。因此,在您的情况下,如果您所做的最后一件事是更改文本框值,则刷新会将文本框再次设置为该值。
不,不会,除非您在更改了值后触发了表单提交文本框。
When you refresh (F5) an ASP.NET page, it will repeat the last action that was taken. So in your case, if the last thing you did was change a textbox value, refreshing will set the textbox to that value again.
No it won't, unless you fired a form submit after you changed the value in your textbox.