RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

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 =)

aown wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 12-30-2008 5:58

thanks....saved a hell lot of time!!!! :-)

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! ;-)

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);

Happouh wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 04-13-2010 10:39

Thank you !!!

Simple, but efficient!

Flemming wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 05-25-2010 13:12

NICE! :D

Kalyanaraman wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-03-2010 10:47

Thanks a lot

ANkita wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-14-2010 18:04

Thank you so much, this works for me.

Wally's World wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-18-2010 22:58

Works for me !

Prax Rellosa wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 07-05-2010 12:28

Great job. It works.

Jessie wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 07-12-2010 15:48

Thank you. You helped me!

haya wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 07-22-2010 14:55

thanks!

how can i get confirm result to code behined by use ScriptManager.RegisterStartupScript?

joe wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 08-12-2010 15:13

I spend 4 hours making it work but this article solved my problem in 4 mins.. thank you

Navaneeth wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 11-14-2010 11:01

Simple but great.... finally got a solution from urs only...

Elver wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 03-24-2011 17:31

TKS.

JotaPardo wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 04-11-2011 21:21

Gracias. Tabién estaba a punto de gritar al cielo, pero gracias a esta buena información aclaratoria me ha funcionado y andado a la perfección mi proyecto usando una MaterPage.!

saludos y gracias

Damir Dobric wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 04-11-2011 23:12

Alles klar, Kumpel :)

Prithu Banerjee wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 05-10-2011 12:36

Very Very Thanks Sir !!!!!!

pubbs wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 05-17-2011 7:22

Thanks Damir..you provide a grt solution... :)

Haly wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 05-25-2011 22:03

You Awesome!

thanks

iMaxHub wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-01-2011 21:12

As it is said.. " Experience is the best teacher.." Good going..!!!

shawn wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-26-2011 8:23

I have to say: You save my ass .. hahaha .. you are the best man ...

Luis wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 07-14-2011 11:03

Awesome, i wasted hours lookinf for a solution.

Thx a lot!!!

jbbi wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 09-22-2011 10:32

Awesome! Thanks !

balaji wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 12-06-2011 10:46

re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!

please help me

Xapax wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 01-02-2012 19:08

Perfect !!!!!!!

Lakshman wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 01-03-2012 8:02

super code.... thank you so much

Fabian reynoso wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 02-28-2012 9:59

Gracias Igual pero no me funciona en

firefox 10.02  probé:

asp.net 2.0 frmamework

3.5 toolkit ajax 3.5

1-

ScriptManager.RegisterStartupScript(Me, Me.GetType(), Guid.NewGuid().ToString(), script, True)

2-

ScriptManager.RegisterStartupScript(Me.updatepanel1, Me..GetType(), Guid.NewGuid().ToString(), script, True)

3-

ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Guid.NewGuid().ToString()", script, True)

4-

ScriptManager.RegisterClientScriptBlock(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), "Guid.NewGuid().ToString()", script, True)

5-

 Dim docGuid As String = Guid.NewGuid().ToString()

                       '  'Do pdf creation stuff here...

                       '  Dim sb As StringBuilder = New StringBuilder("")

                       '  Dim strRoot As String

                       '  strRoot = Request.Url.GetLeftPart(UriPartial.Authority)

                       '  sb.Append("window.open('pdf.aspx?filename=" & Adjunto + "');")

                       '  ScriptManager.RegisterClientScriptBlock(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), "NewClientScript", sb.ToString(), True)

6-

                       ScriptManager.RegisterClientScriptBlock(UpdatePanel1, UpdatePanel1.GetType(), "UpdatePanel1_PreRender", _

                 "<script type='text/javascript' id='UpdatePanel1_PreRender'>" & _

                "include('" & Script & "');" & _

                "removeDuplicatedScript('UpdatePanel1_PreRender');</script>" _

                , False)

Si alguien puedde ayudarme !!  desde ya muchas  gracias !!

fabian.reynoso@gmail.com

binta prasad wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 03-31-2012 11:20

thankyou so.......much...ur post s really useful.it saved my time..

Kumar wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 06-09-2012 11:10

Hi all,

i am using this script in Render() of my page , in this way :

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "blgListPagKey", "SetPageSize(" + this.PaginationSize + ",'#dvBlogListing', '1', '" + currPagCtnrID + "');", true);

Working fine in local machines as well as dev machines , not working in PreProduction Mac ...Is there any way to solve this ?

Toby wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 10-25-2012 11:38

thank you so much!

Roozbeh wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 01-01-2013 6:17

thankyou so.......much...ur post s really useful.it saved my time..

Fernando wrote re: RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
on 02-28-2013 10:14

Does anyone knows a way to make it work in content pages (using masterpages concept)?

The following code just works when the page has not a masterpage.

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "img",       "top.document.getElementById('Image1').src='data:image/jpg;base64," + base64String + "';",

           true);

developers.de is a .Net Community Blog powered by daenet GmbH.