0

I have a Reason to use the Evercookie, It works fine in HTML pages but i am unable to get its value from codebehind after a postback.

I have found a workaround by using a hidden field however it's always loses its value after a postback:

<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> 

In codebehind after a postback i always get val = '' :

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

if (val != "BadRequest") {
}

Please note that the alert shows the value correctly.

What do i missing here?

Community
  • 1
  • 1
Alaa Alweish
  • 8,904
  • 16
  • 57
  • 84

1 Answers1

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

and in the page load event reassign the hidden field value so it won't get lost next postback

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");
        }
      }

   }

}
Siim Nelis
  • 882
  • 7
  • 10
  • It's working, but sometimes it's not.. i still don't know the secret behind it. – Alaa Alweish Nov 18 '13 at 11:01
  • I changed the code so it dont override hfimageurl.Value with empty value if Request.Form("hfimageurl") is null and checks if the values are same it wont change anything – Siim Nelis Nov 19 '13 at 09:45