简体   繁体   English

如何从服务器端按钮单击设置 Javascript Cookie (VB.net)

[英]How To Set Javascript Cookie From Sever Side Button Click (VB.net)

My Problem : I create a server side cookie with a ASP.NET (VB) button click.我的问题:我通过单击 ASP.NET (VB) 按钮创建了一个服务器端 cookie。 The cookie gets created but bc there has only been a page PostBack with the button click, the cookie is not yet recognized by the server side page load code. cookie 被创建,但 bc 只有一个页面PostBack按钮点击,cookie 尚未被服务器端页面加载代码识别。 It will take a page reload (not just a post-back) for the server side cookie to be recognized.需要重新加载页面(不仅仅是回发)才能识别服务器端 cookie。

My Proposed Solution: Create a client side cookie with JavaScript and a server side cookie with VB.net when the ASP.NET button click.我建议的解决方案:当 ASP.NET 按钮单击时,使用 JavaScript 创建一个客户端 cookie,并使用 VB.net 创建一个服务器端 cookie。

Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
 If lblTest.text = "Yes" then

  '1st create client side JavaScript cookie

  '2nd create server side cookie
  Dim bCookie As New HttpCookie("promo")
  bCookie.Value = "samplerpromobox"
  bCookie.Expires = DateTime.Now.AddDays(7)
  Response.Cookies.Add(bCookie)

 End If
End Sub

Questions:问题:

  1. How do I create the client side JavaScript cookie with an ASP.NET (VB) button click?如何通过单击 ASP.NET (VB) 按钮创建客户端 JavaScript cookie?
  2. How do I request the client side cookie from the server side page load on PostBack ?如何从PostBack上的服务器端页面加载请求客户端 cookie?

All help with this is greatly appreciated.非常感谢这方面的所有帮助。

I ended up fixing this issue but not by using my proposed solution.我最终解决了这个问题,但没有使用我提出的解决方案。 I instead used the page's PreRender instead of the page's Page_Load to test for the existence of the just created cookie.我改为使用页面的 PreRender 而不是页面的 Page_Load 来测试刚刚创建的 cookie 的存在。 Apparently the cookies (and some controls) are not immediately available during the the Page_Load but are available (and all controls) during PreRender.显然,cookie(和一些控件)在 Page_Load 期间不是立即可用的,但在 PreRender 期间可用(和所有控件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM