developers.de
daenet's .NET Community

RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!

Imagine you have a control which registers some trivial script, which has to be executed during loading of the page. To do that you will usually use following method:

page.ClientScript.RegisterStartupScript(…)


After some time, you decide to use the same control inside of UpdatePanel and find out that the script is not executed. To understand what the problem is please take a look on the following post. I investigated the problem with the release version of ATLAS and found out that the script is now even not rendered at all. The problem with UpdatePanel and registering of scripts is a little more trivial than expected.

The rendering of scripts by using of AJAX (ATLAS) works different way if UpdatePanel (or AJAX at all) is used. This is, because AJAX uses different rendering methods. For example, consider following code:

 

page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), “alert(‘hello’)”, true);

This code ensures that word ‘hello’ is alerted when the request to page ends up. However, if this code is used inside of UpdatePanel, the message ‘hello’ will not be alerted. To work around the problem there are related static methods in the class ScriptManager which should be used, when the control is used inside of UpdatePanel.
Following example shows how to do that:

 

ScriptManager.RegisterStartupScript(page, page.GetType(), Guid.NewGuid().ToString(), “alert(‘hello’)”, true);

This solution seems to be very trivial. But, image what you have to do if the same control has to be used with and without of UpdatePanel even within the same application. Rick posted one possible solution here.

Posted Feb 12 2007, 02:38 PM by Damir Dobric
Filed under:

Comments

Marco mapick@alice.it wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 04-04-2008 7:09

Wonderfull!!!

I wasted hours, and you solved my problem!

Next time you'll be in Italy, I'll pay you a good drink!

Hi

Damir Dobric wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 04-04-2008 8:18

Welcome. Just send me your coordinates :)

fehro wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 04-27-2008 6:34

Thank you so much, I've been trying to do that for sooooo long =)

rapin wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 01-26-2009 4:22

Thank you so much, this means can solved my problem!

:D

Petr Cpera wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 01-28-2009 4:14

Thanks buddy, you saved me! ;-)

Petr Cpera wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 01-28-2009 4:14

Thanks buddy, you saved me! ;-)

Ivaka wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 01-28-2009 14:34

Thanks!

Mathias wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 03-10-2009 16:40

You saved my day!!!

Thumbs up

Markus wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 05-04-2009 16:10

Thanks guy, I was on the verge of screaming!

Lakshmi wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-15-2009 11:28

is it works  with in masterpage concept also

Hidha wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-19-2009 8:02

Thanks very much. It save me lot of time.

Lucio wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-25-2009 20:21

Nice solution! Congratulations.

Scott Crichton wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 07-16-2009 11:11

Excellent! One simple but effective piece of code. 2 programmers had tried and failed to initialise some javascript. 30 mins of trying and then 1 minute of finding and using your script really helped! Thanks.

Avl wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 08-13-2009 16:08

Thanks very much.

Hozher wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 08-27-2009 18:14

Thanks!!!!

Stephen wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 09-04-2009 16:58

Wonderfull post, first time of ever using Script manager/MS Ajax as i'm too lazy right now to use JQuery for my support system grid. after taking the lazy mans approach and wrapping all grids round Update panel, js scripts weren't been fired! spent 2 hours on this before I stumbled upon this post!

riyaz wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 09-09-2009 12:16

Amazing...thanks a lot....i struggled on this...u saved the day

:)

Nikolai wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 09-09-2009 18:13

GREAT

THANKS A LOT!

In google i found different complex solution, because the peoples just are not learned and exploded the classes and methods in the new .NET Framework

Cecili wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 09-22-2009 17:35

THank you, Thank you, Thank you!!!!!!!!!!

biks wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 10-14-2009 9:24

Thank you so much !!!!!!!!!!!!!!!!

Marhtt wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 12-02-2009 19:10

FANTASTIC! My Christmas has come early!

Don wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 02-05-2010 13:16

Great work !!! HAPPY CODING

Thomas wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 02-07-2010 17:46

Thank You so much!!!! :-)

Fredzool wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 02-24-2010 9:32

you can use

ScriptManager.RegisterStartupScript(this, this.GetType(), "CheckYields", "CheckYields();", true);

UpperAds.com wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 03-11-2010 14:15

2 clicks and it works.

many thanks save alots of time !

Add a Comment

(required)  
(optional)
(required)  
Remember Me?