Search code examples
asp.netwebformshiddenfield

How might I clear a hiddenfield value when the page is refresh in ASP.NET?


How can I clear the value of an asp:HiddenField control when the user hits the refresh button in the browser?


Solution

  • In the load event (Page_Load, OnLoad) set hiddenVariableControl.Value = String.Empty.

    If you are capturing this hidden variable's value for another event, you could do the following in the load event:

    if( !Page.IsPostBack ) 
    {
      hiddenVariableControl.Value = String.Empty;
    }
    

    This sets the value of the hidden variable to a blank string on refreshes, but postbacks (like a button event) would keep the value.