簡體   English   中英

從ASP.NET代碼隱藏中獲取EverCookie的價值

[英]Get EverCookie value from ASP.NET Codebehind

我有一個使用Evercookie的理由 ,它在HTML頁面中可以正常工作,但是回發后我無法從codebehind中獲得其價值。

我通過使用隱藏字段找到了一種解決方法,但是在回發之后,它始終會失去其價值:

<input type="hidden" id="hfimageurl" name="hfimageurl" value="" />
<script>
    var ec2 = new evercookie();
    document.getElementById('hfimageurl').value = "User";
    ec2.get("id", function (value) { if (value == '10') {   
      document.getElementById('hfimageurl').value = "BadUser"; } 
    });

    ec2.get("id", function (value) { alert(value); });
</script> 

在回發后的代碼背后,我總是得到val ='':

string val = Request.Form("hfimageurl");

if (val != "BadRequest") {
}

請注意,警報會正確顯示該值。

我在這里想念什么?

Add runat server to you input
<input type="hidden" id="hfimageurl" name="hfimageurl" value="" runat="server" />

並在頁面加載事件中重新分配隱藏字段值,以便下次回發時不會丟失

protected void Page_Load(object sender, eventargs e)
{   
   if(Request.Form("hfimageurl") != null)
   {

      if(hfimageurl.Value == "")
      {
      hfimageurl.Value=Request.Form("hfimageurl");
      }
      else
      {
        //Check if there is a new value, if is, change the value to new one
        //Otherwise the value should be saved with postback, but it may be lost

        if(!hfimageurl.Value.Equals(Request.Form("hfimageurl"))
        {
           hfimageurl.Value=Request.Form("hfimageurl");
        }
      }

   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM