• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 852
  • Last Modified:

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.

0
mishelper
Asked:
mishelper
1 Solution
 
M3mph15Commented:
Hi,

I believe it is because your javascript is not able to find 'flag'. I'm guessing that the javascript is in the content page code and 'flag' is in the masterpage. If that is so i simple ClientID i don't believe will do the job.

What i would say you do is get the javascript to call a function in the code behind that returns the flag client Id. from the masterpage.

I'm pretty sure i have some example code of this working so once i find it i will post it for you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now