Link to home
Start Free TrialLog in
Avatar of mishelper
mishelper

asked on

How to use javascript to retrieve hiddenfield value in VB.NET?

I put a hidden field with id flag in masterpage to store value ("recchanged"), which reflects changes in the content page. If user closes the page without saving changes, the hiddenfield value in content page will change, and javascript function behind masterpage will be fired.

 

<asp:hiddenfield ID="flag" value=""  runat="server"/>

 

 

-------------------------------------------------------------------------------

 

Code behind Content page:

Changes in textbox will trigger the following event.

 

 

    Protected Sub txt_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt.TextChanged

        Dim hidden As HiddenField

        Session("recchanged") = True

        hidden = Page.Master.FindControl("flag")

        hidden.Value = Session("recchanged")   //here hidden value set to True

End Sub

 

-------------------------------------------------------------------------------

 

If any changes take place in content page, hiddenfield value in masterpage will become True. If user has saved changes, the value will become False.  And the following javascript is supposed to alert user to save changes.

 

 

<script type="text/javascript">

<!--

 

function catchUnload() {

 

 alert(document.getElementById("<%= flag.ClientID%>").value);

 

// if flag.value=true {

//        return "If you click OK you will lose any unsaved changes.";}

}

window.onbeforeunload=catchUnload;

-->

</script>

 

However, when I close the page without saving changes, the value of flag in the alert becomes empty. Anyone know how to solve the problem? Thanks.

ASKER CERTIFIED SOLUTION
Avatar of M3mph15
M3mph15
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial