问题描述:

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?

网友答案:
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");
        }
      }

   }

}
相关阅读:
Top