Click here to Skip to main content
Click here to Skip to main content
Go to top

Adding JavaScript and CSS during an AJAX Partial Postback

, 9 Jan 2009
Rate this:
Please Sign up or sign in to vote.
This article will show how to inject JavaScript and CSS into an AJAX partial postback response.

Introduction

This article will show how to inject JavaScript and CSS into an AJAX partial postback response. If you have started using AJAX, you may have noticed that during an AJAX postback, only items inside the UpdatePanel are refreshed. This is known as an AJAX Partial Postback or an AJAX Partial Page Rendering. If you are programmatically inserting code such as custom JavaScript or CSS for a user control you have inside of an UpdatePanel, you may have noticed that the CSS or JavaScript was not sent back to the client's browser during the partial postback response. This would apply to new code being sent just in the partial postback response.

Problem Analysis

The above mentioned scenario is occurring because some of the old methods we used to programmatically insert code into a postback no longer work with an AJAX partial postback. These methods were developed before AJAX came into the picture. Examples of objects that will not work during an AJAX partial postback are:

// ClientScript (ClientScriptManager class)
// will not work on an ajax partial postback
ClientScript.RegisterStartupScript(...)     
// Page.Header.Controls.Add will not work on an ajax partial postback 
Page.Header.Controls.Add(...);

Solution

The solution is called ScriptManager. ScriptManager methods are basically the same, but take an additional parameter of either the page or a control. The ScriptManager will send everything back to the client during an AJAX partial postback. So, if we need to inject JavaScript, we must take advantage of the ScriptManager object.

Example 1: Inserting JavaScript

To insert JavaScript, use the RegisterStartupScript method as follows:

// Javascript "Hello World" alert box using RegisterStartupScript 
ScriptManager.RegisterStartupScript(this,
                                    typeof(nameOfPageOrControl),
                                    "typeAUniqueScriptNameHere1",
                                    "alert('Hello World');", true);

Notice if you are placing the script inside a user control, nameOfPageOrControl would be the name of the user control, otherwise the page class name.

Example 2: Inserting a JavaScript Include File

To insert a JavaScript include file, use the RegisterClientScriptInclude method as follows:

// Register a Javascript include file during a partial postback
ScriptManager.RegisterClientScriptInclude(this,
                                          typeof(nameOfPageOrControl),
                                          "typeAUniqueScriptNameHere2",
                                          ResolveUrl("~") + "myscript.js");

Again, if you are placing the script inside a user control, nameOfPageOrControl would be the name of the user control, otherwise the page class name.

Example 3: Inserting a CSS Include File

To insert a CSS include file, use the RegisterClientScriptBlock method as follows:

// Register a CSS file during a partial postback
ScriptManager.RegisterClientScriptBlock(this,
                                        typeof(nameOfPageOrControl),
                                        "typeAUniqueScriptNameHere3",
                                        "<link href='" +
                                        ResolveUrl("~") +
                                        "mystyle.css' rel='stylesheet' type='text/css' />",
                                        false);

Again, if you are placing the script inside a user control, nameOfPageOrControl would be the name of the user control, otherwise the page class name. Also, notice the last parameter is set to false. When this parameter is false, the script blocks are not placed in the code since CSS does not use script blocks.

Word of Caution Regarding CSS and the Example 3 Code

It is possible in some scenarios that Example 3 above needs to be wrapped in script tags and the last parameter to add the script tags be set to true instead of false:

string script1;
string script2;

script1 = "<link href='" +
          ResolveUrl("~") +
          "mystyle.css' rel='stylesheet' type='text/css' />";

script2 = "//]]></script>" +
          script1 +
          "<script type=\"text/javascript\">//<![CDATA[";

ScriptManager.RegisterClientScriptBlock(this,
                                        typeof(nameOfPageOrControl),
                                        "typeAUniqueScriptNameHere3",
                                        script2,
                                        true);

Points of Interest

It is very important that the key parameter (the last parameter in the call to RegisterClientScriptBlock) be unique. In the above examples, I used different key names such as typeAUniqueScriptNameHere1. Make sure this key is different, or your scripts will get overridden with other scripts. I hope this article will be of help in programmatically placing code into AJAX partial postbacks.

History

  • 1.0 - 01/09/2008 - Original version.
  • 1.1 - 01/09/2008 - Added comment regarding example 3 above.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SeaWater
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionExample 3 seems to be not working [modified] Pinmembernk11113-Aug-14 21:21 
GeneralMy vote of 5 PinmemberDominick O'Dierno4-Oct-10 12:40 
Generalrunning-specific-javascript-after.html Pinmembergo2amitech7-Aug-10 4:38 
GeneralI use sys class PinmemberMahir7819-Jan-09 19:30 
GeneralMy vote of 1 PinmemberAdnan Aman15-Jan-09 0:17 
GeneralRe: My vote of 1 PinmemberSimon Capewell20-Jan-09 1:15 
GeneralVery Helpfull PinmemberWebplethora14-Jan-09 18:43 
GeneralRe: Very Helpfull PinmemberSeaWater15-Jan-09 2:34 
GeneralHelpful Pinmemberrajnikantv12-Jan-09 22:28 
GeneralMy vote of 1 PinmemberRogic10-Jan-09 9:04 
GeneralRe: My vote of 1 PinmemberGevorg12-Jan-09 3:46 
GeneralRe: My vote of 1 PinmemberWhiteRose161113-Jan-09 2:32 
GeneralRe: My vote of 1 Pinmembergajathegreat13-Jan-09 6:50 
AnswerRe: My vote of 1 PinmemberKanedogg0815-Jan-09 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 9 Jan 2009
Article Copyright 2009 by SeaWater
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid