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

Posted on 2009-04-28
Last Modified: 2012-08-13
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.";}






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.

Question by:mishelper
    1 Comment
    LVL 6

    Accepted Solution


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now