我如何通过的Response.Redirect坚持一个标签的价值? [英] How do I persist the value of a label through a response.redirect?

查看:113
本文介绍了我如何通过的Response.Redirect坚持一个标签的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的情况:我有一个标签的文本集,后面紧跟的Response.Redirect()调用如下(这只是一个例子,但我相信它准确地描述了我的情况):

Here's the situation: I have a label's text set, immediately followed by a response.redirect() call as follows (this is just an example, but I believe it describes my situation accurately):

ASPX:

<asp:Label runat="server" Text="default text" />

code-背后(叫上一个onclick事件code):

Code-behind (code called on an onclick event):

Label.Text = "foo";
Response.Redirect("Default.aspx");

在页面呈现,标签上写着默认文本。什么我需要做不同?我的理解是,这些改变将自动地在幕后进行,但很显然,不是在这种情况下。谢谢你。

When the page renders, the label says "default text". What do I need to do differently? My understanding was that such changes would be done automatically behind the scenes, but apparently, not in this case. Thanks.

有关一点点额外的背景下,code-背后片段是称那是在onclick事件调用的方法内。还有更多的,但我只包括了其中感兴趣的是这个问题。

For a little extra background, the code-behind snippet is called inside a method that's invoked upon an onclick event. There is more to it, but I only included that which is of interest to this issue.

推荐答案

重定向后,您将失去关联到你的控件的状态信息。如果您只是想页面刷新,删除重定向。在code执行完毕后,页面将刷新任何状态还将继续。

After a redirect you will loose any state information associated to your controls. If you simply want the page to refresh, remove the redirect. After the code has finished executing, the page will refresh and any state will be kept.

在幕后,这工作,因为ASP.NET写入的状态信息到一个隐藏的输入字段在页面上。当你点击一个按钮,表单发布和ASP.NET解密的视图状态。您code运行时,修改状态,之后,状态再次写入隐藏字段和循环下去,在您更改页面没有POST 。点击超链接的另一页时,会发生这种情况,或通过的Response.Redirect(),它指示浏览器按照指定的URL。

Behind the scenes, this works because ASP.NET writes the state information to a hidden input field on the page. When you click a button, the form is posted and ASP.NET deciphers the viewstate. Your code runs, modifying the state, and after that the state is again written to the hidden field and the cycle continues, until you change the page without a POST. This can happen when clicking an hyperlink to another page, or via Response.Redirect(), which instructs the browser to follow the specified url.

这篇关于我如何通过的Response.Redirect坚持一个标签的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆