Injecting client script into Infragistics async postback

Often there is a need to add client-side Javascript to the ASP.NET page from server-side code. To perform additional manipulation on rendered controls (hide or disable), to show user an alert message – just a couple of examples. Standard ASP.NET approach is to use page’s client script manager’s  RegisterStartupScript method:

Me.ClientScript.RegisterStartupScript (Type,  Key,  Script, AddScriptTags)


type: The type of the startup script to register.
key: The key of the startup script to register.
script: The startup script literal to register.
addScriptTags: A Boolean value indicating whether to add script tags.

For example:

Me.ClientScript.RegisterStartupScript (Me.GetType(), "alert", "alert('Hello world!');", True)

But if you’re using Infragistics controls that offer async postbacks, like WARP panel or UltraWebTab – there is a problem with this approach.  Since the page doesn’t go through the full postback and doesn’t get destroyed and re-rendered from scratch – this method doesn’t work.

One approach is to create a literal control (either static in ASPX markup or dynamic thru code) and assign it the code that needs to be executed on the client:

xltScript.text = "<script type='text/javascript'>alert('Hello world!');</script>"

It works, but it can become messy and hard to maintain.  A much better approach is to use Infragistic’s CallBackManager’s AddScriptBlock method:

AddScriptBlock(page, ownerOfAsyncPostBack, javaScript)


page: Reference to Page.
ownerOfAsyncPostBack: Control, caused async postback. If that parameter is Nothing, then script is added regardless of the owner of postback.
javaScript: Javascript statements.

For example:

Infragistics.WebUI.Shared.CallBackManager.AddScriptBlock(Me, Nothing, "alert('Hello World!');")

To make it universal we can combine both original ASP.NET approach for full postback and the Infragistics method for async postback into a single function:

Public Sub RunJavascript(ByVal i_oPage As Page, ByVal i_sJScode As String)
   If Infragistics.WebUI.Shared.CallBackManager.IsAsyncPostBack(i_oPage) Then
      Infragistics.WebUI.Shared.CallBackManager.AddScriptBlock(i_oPage, Nothing, i_sJScode)
      i_oPage.ClientScript.RegisterStartupScript(i_oPage.GetType, "JSCode" & Rnd(1).ToString, i_sJScode, True)
   End If
End Sub

Here, the function RunJavascript (which can reside in a standalone class, separate from the page) accepts 2 parameters: the reference to ASP.NET page and the string with JavaScript statements. First it checks whether the page is in async postback mode (Line 2, IsAsyncPostBack method) and if so – performs Infragistics AddScriptBlock method. Otherwise it peforms standard ASP.NET RegisterStartupScript method (note randomizer used for the key parameter, this is done so if you call this method several times in a row, all JavaScript statements will added, otherwise only the last one will remain).

Therefore we combine examples from above into a single call:

RunJavascript(Me, "alert('Hello world!');")

and the JavaScript code will be added to the page no matter whether it is first load, full postback or Infragistics async postback. Don’t forget to add semicolumn at the end of your JavaScript statement, in case more statements are added later on (otherwise you may get a syntax error when client script tries to execute).

3 replies on “Injecting client script into Infragistics async postback”

  1. Hi,

    May I ask you a question?

    I have a linkbutton in the WARP panel, and when click this linkbutton it will Response.Redirect(“abc.aspx”) redirect to another page, but this action won’t works when I use infragistics WARP panel control. I try to use the above method you mentioned.

    Infragistics.WebUI.Shared.CallBackManager.AddScriptBlock(this, null, “{’)}”);

    But it still don’t work. I don’t know why, could you please let me know how could I make this Response.Redirect() works against WARP panel control.

    Thank you so much!!!


  2. Hi,

    I found the problem. I have some code in another events block this CallBackManager work.



  3. @Ruth
    Glad you found the problem 🙂 By the way, if you want to simulate Response.Redirect (i.e. open the link in the same window) you can use “location.href” approach:

    Infragistics.WebUI.Shared.CallBackManager.AddScriptBlock(this, null, “location.href=abc.aspx;”);

Leave a Reply

Your email address will not be published. Required fields are marked *