Solved

setting hidden field from code behind on a way that the onchange attivute will fire.

Posted on 2008-10-20
3
1,305 Views
Last Modified: 2012-05-05
I have an  <asp:textbox> on my form that I  set the value of when a user clicks a  submit button.   I have added an onchange attibute to this textbox .  

        this.txtNeedsVersion.Attributes.Add("onchange", "SaveVersion.globalChange = true;");

I have used ClientScript.RegisterOnSubmitStatement (...)  to ensure that when a change occurs that a flag is set  and I have

// Register the event.
window.onbeforeunload = function() {
    return SaveVersion.checkForChanges();

to ensure that some javascript will run  onbeforeunload    

As long as I  type a value into the textbox I added  everything works fine.  Unfortunately when I set the value in the code behind  the onchange is not fired or it is not capture  and as a result my onbeforeunload  script is not run.  

How can I get this to work?  
0
Comment
Question by:DylanJones1
3 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22759280

window.onbeforeunload = function() {
    SaveVersion.globalChange = true;
    return SaveVersion.checkForChanges();

Open in new window

0
 
LVL 6

Accepted Solution

by:
AmarIs26 earned 500 total points
ID: 22759305
You can declare a string variable which you assign the value at onload. This value is the value of your textbox. Then when you get to prerender event you can compare the variable value to the current textbox.text value to find out if they have changed or not? If it is different then you can assume that the value has been changed by some method.
At this stage you can register some javascript which will set your
SaveVersion.globalChange = true;
This way the page is now loaded with the flag set which will fire your unload event handler on client side ;)
0
 

Author Closing Comment

by:DylanJones1
ID: 31507865
Thanks Amarls
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

825 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