饼干只显示在刷新? [英] Cookie only displayed on refresh?
问题描述
我有一些麻烦,所以在这里它是理解这一点。
I have some trouble understanding this one so here it is.
我试图设置cookie,并显示使用ASP.NET + C#在页面上的价值。
这是我的code:
protected void lbChangeToSmall_Click(object sender, EventArgs e)
{
Response.Cookies["fontSize"].Value = "small";
}
和
<asp:LinkButton runat="server" id="lbChangeToSmall" Text="A" CssClass="txt-sm" OnClick="lbChangeToSmall_Click"></asp:LinkButton>
最后
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write( Request.Cookies["fontSize"].Value);
}
}
当我按一下按钮,没有显示在页面上,但该cookie将被实际设置。如果我刷新页面,则cookie显示。
When I click on the button, nothing is displayed on the page, but the cookie is actually set. If I refresh the page, the cookie displays.
如此看来,该cookie设置正确,但应用程序不能读它的时候了。
So it seems that the cookie is set correctly but the application is not able to read it right away.
我试图摆脱的,如果(回传):
I tried to get rid of the if(postBack):
protected void Page_Load(object sender, EventArgs e)
{
Response.Write( Request.Cookies["virgilFontSize"].Value);
}
但它并没有改变任何事情。
but it didn't change a thing.
我在做什么错了?
谢谢!
推荐答案
的 lblChangeToSmall_Click
事件后烧制的的Page_Load
事件。因此,写的cookie将不提供该请求,直到随后的回发。
The lblChangeToSmall_Click
event is fired after the Page_Load
event. Therefore the cookie write won't be available on the Request until the subsequent postback.
这将是avaialable在客户端上,虽然立即生效。
It will be avaialable on the client immediately though.
这篇关于饼干只显示在刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!