Answered by:
Ajax UpdatePanel and onload=JavaScript Function

Question
-
User1539122883 posted
Hi,
I am trying to work through a program but getting really stuck.
I have a asp.net (aspx) page which shows a table of data from a SQL query to the DB. This all works fine. I have a <body onload='resizediv()';> which calls a JavaScript code that resizes the DIV that the table data is displayed in - just so it adds a scrolling area if the table data is >400px deep.
This all works just fine and as it should.
So I thought I would Ajax enable it with a panelupdate control which I wrapped around the Table and DIC that displays the data from the DB. Again this all works as it should - clicking on my "refresh" control button the ajax re-loads the table data without the nasty page flicker.
BUT - my onload='resizediv();' JS function is not called when the updatepanel is refreshed ... it is called the first time the page loads but not when I click on the button control that does the refresh of the panel. Is there anyway to make this JS function fire each time the Ajax panel is updated ?
Apologies if this description is hard to understand, but I am very new to asp.net and Ajax .... :(
Thanks in advance ....
Ro
Wednesday, February 28, 2007 7:30 AM
Answers
-
User902366211 posted
Instead of using onload=..., try:
<script type="text/javascript">
Sys.Application.add_load(function() { ... });
</script>If memory serves, the Load event should fire on initial page load as well as any async postbacks.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 28, 2007 6:20 PM
All replies
-
User1539122883 posted
No ideas on how this could be done ? or am I just looking at this from the wrong angle ?
Thanks
Ro
Wednesday, February 28, 2007 5:09 PM -
User902366211 posted
Instead of using onload=..., try:
<script type="text/javascript">
Sys.Application.add_load(function() { ... });
</script>If memory serves, the Load event should fire on initial page load as well as any async postbacks.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 28, 2007 6:20 PM -
User1539122883 posted
Steve,
Excellent reply, just tried this and indeed it works with load and partial page loads.
This will help me out no-end .....
Many thanks,
Ro
Thursday, March 1, 2007 6:07 AM -
User-480136453 posted
Instead of using onload=..., try:
<script type="text/javascript">
Sys.Application.add_load(function() { ... });
</script>If memory serves, the Load event should fire on initial page load as well as any async postbacks.
Hello , I try to use it but I got his javascript error
'sys' is undefiend
So any help, plz reply me
thanks a lot.
Saturday, May 24, 2008 10:46 AM -
User-1949782799 posted
This is probably because you are calling Sys.Application.add_load before the asp.net ajax script reference in the html.
Try placing the following line in your Page_Load handler in the code-behind:
ClientScript.RegisterStartupScript(this, "addload", "Sys.Application.add_load(resizediv);", true);Another option is to rename your function to pageLoad.
Any functions with this name will be called automatically by asp.net ajax if it exists on the page, also after each partial update.
If you do this you can also remove the onload attribute from the body tagRegards,
Einar
Monday, May 26, 2008 4:57 AM -
User-480136453 posted
Hello , I did it ,
but is there ny way to call a javascript function after the page_load event ?
I do something in code in page_load , and I want to call a javascript function after page_load
is there any way to do that !!
thanks a lot.
Thursday, May 29, 2008 4:26 AM -
User-1949782799 posted
Do you mean after the Page_Load serverside event?
This will invoke a javascript once the page has rendered (the html has been sent to the browser):
ClientScript.RegisterStartupScript(this.GetType(), "pagestart", "someFunction();", true);Thursday, May 29, 2008 5:10 AM -
User-480136453 posted
Well , thank you
finally I get it
by the way I found something strange ,
I put a label and change it's value in page load , then I call this javascript function to show it's contents
the result is that 'undifiend'
but when I use TextBox instade of label , it works very well , I don't know why , but it works , any one knows why ?
Thursday, May 29, 2008 6:31 AM -
User-1949782799 posted
A bit hard to answer that without seeing the javascript you are using.
But if you are using myLabel.value in javascript, that will return undefined since a label element does not have value property in dhtml.
Only fields that can be submitted to the server have a value property (textbox, checkbox, radiobutton, textarea, dropdown, etc)For label you should use myLabel.innerHTML, same if using a span or a div, etc.
Thursday, May 29, 2008 7:21 AM -
User-480136453 posted
Only fields that can be submitted to the server have a value property (textbox, checkbox, radiobutton, textarea, dropdown, etc)
For label you should use myLabel.innerHTML, same if using a span or a div, etc.
thanks for your reply, really good
my problem solved , thanks a lot
Thursday, May 29, 2008 9:03 AM -
User-2052661379 posted
This is a really great. Was very helful.
Wednesday, July 1, 2009 2:38 AM -
User-1219536331 posted
Steve Marx, Thanks very much. It really helped me a lot.
Monday, February 8, 2010 3:44 PM -
User1376332077 posted
Hello,
Its Gr8... I tried it Out in both the Mozill Firifox and IE and it looks to be Great.
Thanks
Monday, June 28, 2010 6:44 AM -
User-1604093243 posted
Hi ,
Great post...It worked me... I was breaking my head for a long time.!!
Thank you!
Friday, August 20, 2010 12:44 AM -
User-1667217862 posted
You Rocked Steve Marx,
Thank you so much for ur support
Wednesday, January 5, 2011 4:51 AM -
User-1667217862 posted
Steve Marx,
Thanks a bundle
Wednesday, January 5, 2011 4:53 AM -
User889521826 posted
The provided solution did not work for me because the ClientScript.registerStartupScript was not registering the script; in case you have the same problem try with ScriptManager.RegisterStartupScript.
Thanks Steve.
Tuesday, January 18, 2011 3:46 PM