Call javascript function when consuming event in codebehind - javascript

I have a aspx page that, when loading, will initialize a class called, Bill :)
When Bill is initialized, the aspx page will wire a function to a handler of Bills.
When that function in Bill is fired, I would like to call a javascript function in the aspx page, but that is where I am having difficulties...
I have tried ClientScript.Register and all of that, but nothing is seeming to work. As of now, I am simple trying to get the function to fire an alert.
I have a feeling that I am missing something critical in the understanding of this, so any information would be greatly appreciated.
Thanks.

Is all of this happening in javascript, or is some of this server-side code? Javascript is client-side only, so if you are running server-side code, you can't execute the javascript on the server. You can, however, register script to run when the page renders in the browser (be careful of this, if you are opening another window, actions like this will be blocked by the browser unless the user directly initiates the action by clicking a button or something similar).

Related

Invalid Postback or Callback Error - How to find what is causing the error

I'm working on an asp.net web application and am running into this error after manipulating some controls client-side (html buttons nested in update panels):
"Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."
The stack trace given is:
"[ArgumentException: Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.]
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +9832822
System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +114
System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) +65
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +18
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +471
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1897"
This stack trace is useless in that it's not telling me what control is causing the issue. I think I understand the basics of what the error is trying to say, but without a direction to go it's a total crapshoot as to where to start looking, particularly since I'm not manipulating any dropdown lists.
Before I go through the hassle of posting code, how can I get the complete stack trace (showing file and line number) for the error to figure out what is happening? I've been working on this problem for a week now and I'm pretty well fed up with it. Thanks.
hum, does this occur on a button click? Try moving any control validators to the top of the page, see if that helps. I guess a few more details as to "when" the error occurs. (does it occur on first page load, or due to some button click? And do you have any say JavaScript bits and parts that might have posted the page back before it is 100% rendered? I mean, something added to the page must have "started" this issue to occur. but, we quite much guessing. You could as a test in web config turn off page validation, and at least see if that works, and then try to fix and then re-enable.
Also, are you loading up data into some controls on page load?
Try then placing that "setup" and "data loading" inside of the !IsPostback (which all pages quite much need and have, right??).
So, in page load event, place that data load, and setup code you have inside of the !IsPostBack block like this:
if (!IsPostBack)
{
page load + setup + data loading code goes here
}
So, adopt the above if you have code to load up say a gridview, or even a dropdown box. I seen the above "simple" change in code fix your error, and a good number of times.

With the Javascript event onbeforeunload, is it possible to do anything other than an alert dialog?

In all of the sample code I have seen, it appears that the only function of onbeforeunload is to serve up an alert dialog box prior to the person leaving the page. Is that the only thing that can be triggered by the event or is it possible to do something else, like an unobtrusive function that sends off partial form data?
I am trying to capture abandoned shopping carts in Yahoo! Small Business and unfortunately I do not have access to any server side scripting, so I'm forced to work client-side only.
I was also thinking of doing an ajax posting of data after the email field was changed, and then comparing the list of all forms partially submitted against completed carts to determine which were incomplete.
You can save the partial form data in localStorage. Then, when another page is loaded, you could check for the presence of that data and AJAX it to the server, deleting it from localStorage on success. Or you might be able to just use that data in JavaScript, without involving the server, but that depends on your setup.
<body onbeforeunload="return ('You will lose all your data')" onunload="alert('You have gone away!')">
</body>
Onbeforeunload uses for alert box. Onunload for anything else.
You can technically fire off an ajax event, but there is no guarantee that it will complete before the page is actually reloaded.

passing parameters from js to .aspx page using windows location

I can pass parameters from javascript function to an aspx file. I use windows.location.href for that.
When the execution gets to the aspx page, it runs and it tries to call multiple WCF services using those parameters. But the moment it calls the first one, the IE window on the back takes me to the root view that lists all available aspx pages.
i've tried setting a breakpoint on the page load that is receiving those parametrs. I try to step through the codes, it will let me do that just fine but when it calls the first WCF service, it will do the above to the IE window BUT it will still let me continue steppng through the code after that.
IE window should be waiting for me until I am done executing code on the server, right?
It has something to do with the combination of WCF calls & JS window.location.href statemetn. I know that for sure. It consistently is doing that at a specific line of code (the first WCF call).
I don't see this behavior if for instance, I try to pass the parameters in a session variable coming from another .aspx page.
Has anyone seen this behavior? do you know what is going on?
Thanks
--- REPHRASED---
thank you for the feedback.
hari, I am doing this from my local machine running VS 2008. I do use forms authentication.
Maybe you didn't get my questions, because I didn't phrase it properly like Diodeus thinks, so let me try again:
I collect input values form one page using JS. Then I need to pass these parameters to an aspx page using windows.location.href.
I set up a breaking point at the page load event to make sure that execution is reachign the aspx page. and it does.
This page successfully receives those variables and does more processing like calling external WCF services. At this point the IE window is locked and becomes unviewable because the execution is still taking place. This is normal (is this part that throw you off?).
The moment I start executing a call to the WCF service, the IE window finishes loading and it takes me to the direct browsing WHILE I am still debugging inside visual studio (like a line of code beomes highlighted in yellow). I can continue stepping throw using F10 or F11, but my IE window on the back is done!? It should continue waiting for me until all lines of codes finish processing.
I hope this helps. Pls let me know if it does and be able to help me figure it out.
Disable directory browsing for that website (at IIS), and set a default page for the website (also at IIS).

How to run a javascript function when ANY page is loaded

I am doing some maintenance work to a fairly large existing site written in PHP & JavaScript. I am trying to set up a "time out" so that the user is automatically logged out after a specified period of time. I think I have the code that will "expire" the users session, but what I need is a way to run a specific javascript function whenever ANY of the pages in the existing system are loaded. I'd rather not manually add a call in each page as this would take forever and would require even more testing.
Is there a way to add a javascript function call to the window or some other part of the DOM to get called whenever a page is loaded?
Thanks for any help!
There are many ways to achieve this. BUT, you will have to first include a reference to the javascript file.
Then, you can, for instance, use jQuery to detect that the DOM is loaded and ready to call a function of yours.
On a side note, can I ask you why you need to call a javascript function? There are probably other ways to do that, like a listener on your server that redirects to a logout page when a session expires.
Write the javascript in a .js file, host it on your server, and link to the .js from all the pages. While this does not apply a global rule, it is the only way I can think of and it won't be a problem for testing as the code will be from one source.
No.
You have to have the javascript function load into every page. But you just have to write it once and then include like:
<script src="logout.js"></script>
and then you'll need to set the timer for the logout
<body onload="setLogoutTimer()">
But in order for every page to have it you either need to explicitly place it in every page.
That depends to your site script. If you're checking your session data on every request this could be done easily.
Add time data for a session's last move, append it to a js script which controls that If it's time to end the session or not. And take action(js redirect or an ajax request)

How to make sure that a Javascript function is called after EVERY postback in Liferay/ICEFaces?

I have very little experience with Liferay and ICEFaces. I need to build a portlet that calls a specific Javascript function every time a postback occurs.
In fact, when the form is submitted through AJAX, a loading screen appears and blocks the screen. When the postback completes, I need to take it out and give control to user again.
I have tried to put the <script> snippet in the portlet's body and it succeeds in executing when the page is first loaded (it's ok) and also when a postback changes the page's HTML content.
The problem is, when the server doesn't change the HTML (ie. a search returned with no result, you search again and you still get no results) the function is not executed again.
Even if I could post some code, I have very little idea of the architecture the web application is built on. What I gave here is all the information I have. By editing the javascript function to emit alerts when it's called I found the behaviour of the page.
Can you help me with that?
Have you tried the following:
<script>
Ice.onSendReceive('document:body',
function(){/*Before send ajax request*/},
function(){/*After receive the response*/}
);
</script>
But this solution applied for every ajax request you made, hope this can help you.
I made it using a weird trick with random numbers I want to share.
First, in the backing bean property I created a method
public String getLoadFunction()
{
return "functionName("+ new Random().nextDouble() +");";
}
and then
<script>
#{MyViewBean.loadFunction}
</script>
"Simply", ICEFaces is so smart it checks if the HTML has been modified or not when processing AJAX requests. This trick is very similar to appending a random number to HTTP URLs to avoid caches.

Categories

Resources